index.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. var util = require('../../../utils/util.js');
  2. var api = require('../../../config/api.js');
  3. var user = require('../../../services/user.js');
  4. var app = getApp();
  5. // 定义插件变量 引入方法
  6. const tempUserInfo = {
  7. nickName: 'Hi,游客',
  8. userName: '点击去登录',
  9. avatarUrl: 'https://platform-wxmall.oss-cn-beijing.aliyuncs.com/upload/20180727/150547696d798c.png'
  10. }
  11. Page({
  12. data: {
  13. userInfo: {},
  14. hasMobile: '',
  15. imgServer: util.imgServer,
  16. all: {
  17. icon: 'ct1',
  18. name: '全部订单',
  19. id: -1
  20. },
  21. orderList:[
  22. {
  23. icon: 'ct2',
  24. name: '待付款',
  25. id: 0
  26. },
  27. {
  28. icon: 'ct3',
  29. name: '待发货',
  30. id: 201
  31. },
  32. {
  33. icon: 'ct4',
  34. name: '待收货',
  35. id: 300
  36. },
  37. {
  38. icon: 'ct5',
  39. name: '已完成',
  40. id: 301
  41. }
  42. ],
  43. barList:[
  44. {
  45. icon: 'cb1',
  46. url: '/pages/ucenter/collect/collect',
  47. name: '我的收藏',
  48. id: 0
  49. },
  50. {
  51. icon: 'cb5',
  52. url: '/pages/coupon/coupon',
  53. name: '优惠券',
  54. id: 5
  55. },
  56. // {
  57. // icon: 'cb6',
  58. // url: '/pages/ucenter/footprint/footprint',
  59. // name: '我的积分',
  60. // id: 1
  61. // },
  62. {
  63. icon: 'cb2',
  64. url: '/pages/ucenter/address/address',
  65. name: '地址管理',
  66. id: 1
  67. }
  68. ],
  69. relatedGoods: [],
  70. isLogin: app.globalData.isLogin
  71. },
  72. getGoodsRelated: function () {
  73. let that = this;
  74. util.request(api.GoodsRelated, {}).then(function (res) {
  75. if (res.errno === 0) {
  76. that.setData({
  77. relatedGoods: res.data.goodsList,
  78. });
  79. }
  80. });
  81. },
  82. tapByIdx:function(e){
  83. let {
  84. url
  85. } = e.currentTarget.dataset;
  86. console.log(app.globalData)
  87. // if (this.data.isLogin) {
  88. wx.navigateTo({
  89. url: url,
  90. success: function (res) { },
  91. fail: function (res) { },
  92. complete: function (res) { },
  93. })
  94. // } else {
  95. // wx.navigateTo({
  96. // url: '/pages/auth/btnAuth/btnAuth',
  97. // })
  98. // // wx.showModal({
  99. // // title: '提示',
  100. // // content: '请登录后查看',
  101. // // showCancel: false,
  102. // // confirmColor: '#ED5D18',
  103. // // success: res=>{
  104. // // }
  105. // // });
  106. // }
  107. },
  108. onLoad: function (options) {
  109. getApp().checkNetStatu();
  110. // 页面初始化 options为页面跳转所带来的参数
  111. // this.getGoodsRelated()
  112. // wx.setNavigationBarColor({
  113. // frontColor:'#ffffff',
  114. // backgroundColor:'#fb6400',
  115. // "navigationStyle":"custom"
  116. // animation: {
  117. // duration: 400,
  118. // timingFunc:'easeIn'
  119. // }
  120. // })
  121. },
  122. onReady: function () {
  123. },
  124. async getCounts() {
  125. let { data } = await util.request(api.OrderCount)
  126. let orderList = this.data.orderList.map(item => {
  127. let ci = data.find(({code}) => code === item.id)
  128. if (ci) {
  129. item.count = ci.num > 99 ? '..' : ci.num
  130. }
  131. return { ...item }
  132. })
  133. this.setData({ orderList })
  134. },
  135. async getUserInfo() {
  136. const {data} = await util.request(api.UserInfo)
  137. wx.setStorageSync('userinfoDetail', data)
  138. this.setData({
  139. userInfo: {
  140. nickName: data.nickname,
  141. avatarUrl: data.avatar,
  142. }
  143. })
  144. },
  145. async getBrandList () {
  146. let res = await util.request(api.GetAdminBrands)
  147. let list = res.data
  148. if (list && list.length>0) {
  149. this.setData({
  150. hasBrand: true
  151. })
  152. } else {
  153. this.setData({
  154. hasBrand: false
  155. })
  156. }
  157. },
  158. onShow: function () {
  159. getApp().updateCardCount()
  160. let userInfo = wx.getStorageSync('userInfo');
  161. let token = wx.getStorageSync('token');
  162. this.getBrandList()
  163. this.getUserInfo()
  164. this.getCounts()
  165. // 页面显示
  166. if (userInfo && token) {
  167. app.globalData.userInfo = userInfo;
  168. app.globalData.token = token;
  169. }
  170. this.setData({
  171. // userInfo: app.globalData.userInfo,
  172. token: app.globalData.token
  173. });
  174. util.request(api.CartList).then((res) => {
  175. let isLogin = false
  176. if (res.errno === 401) {
  177. isLogin = false
  178. app.globalData.userInfo = tempUserInfo;
  179. app.globalData.token = '';
  180. } else {
  181. isLogin = true
  182. app.globalData.userInfo = userInfo;
  183. app.globalData.token = token;
  184. }
  185. if (!isLogin){
  186. this.setData({
  187. token:'',
  188. userInfo: tempUserInfo
  189. });
  190. }
  191. this.setData({
  192. isLogin
  193. });
  194. });
  195. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  196. this.getTabBar().setData({
  197. selected: 3
  198. })
  199. }
  200. },
  201. onPullDownRefresh() {
  202. getApp().onPullDownRefresh()
  203. },
  204. onHide: function () {
  205. // 页面隐藏
  206. },
  207. onUnload: function () {
  208. // 页面关闭
  209. },
  210. bindGetUserInfo(e) {
  211. let userInfo = wx.getStorageSync('userInfo');
  212. let token = wx.getStorageSync('token');
  213. if (userInfo && token) {
  214. return;
  215. }
  216. if (e.detail.userInfo){
  217. //用户按了允许授权按钮
  218. user.loginByWeixin(e.detail).then(res => {
  219. this.setData({
  220. userInfo: res.data.userInfo,
  221. isLogin: true
  222. });
  223. res.data.userInfo.userId = res.data.userId
  224. wx.setStorageSync('userInfo', res.data.userInfo);
  225. wx.setStorageSync('token', res.data.token);
  226. wx.setStorageSync('userId', res.data.userId);
  227. app.globalData.userInfo = res.data.userInfo;
  228. app.globalData.token = res.data.token;
  229. }).catch((err) => {
  230. this.setData({
  231. token: '',
  232. userInfo: tempUserInfo
  233. });
  234. app.globalData.userInfo = tempUserInfo;
  235. app.globalData.token = '';
  236. console.log(err)
  237. });
  238. } else {
  239. //用户按了拒绝按钮
  240. // wx.showModal({
  241. // title: '温馨提示',
  242. // content: '您点击了"取消"授权,将无法获取看店4DKanKan完整服务。请“授权”以保证各项功能正常使用。',
  243. // confirmText:'授权',
  244. // success: function (res) {
  245. // if (res.confirm) {
  246. // wx.openSetting({
  247. // success: (res) => {
  248. // if (res.authSetting["scope.userInfo"]) {////如果用户重新同意了授权登录
  249. // user.loginByWeixin(e.detail).then(res => {
  250. // this.setData({
  251. // userInfo: res.data.userInfo
  252. // });
  253. // app.globalData.userInfo = res.data.userInfo;
  254. // app.globalData.token = res.data.token;
  255. // }).catch((err) => {
  256. // console.log(err)
  257. // });
  258. // }
  259. // }
  260. // })
  261. // }
  262. // }
  263. // });
  264. }
  265. },
  266. exitLogin: function () {
  267. wx.showModal({
  268. title: '',
  269. confirmColor: '#b4282d',
  270. content: '退出登录?',
  271. success: function (res) {
  272. if (res.confirm) {
  273. wx.removeStorageSync('token');
  274. wx.removeStorageSync('userInfo');
  275. wx.switchTab({
  276. url: '/pages/index/index'
  277. });
  278. }
  279. }
  280. })
  281. },
  282. toReport () {
  283. wx.navigateTo({
  284. url: '/dataReport/pages/data-report/data-report',
  285. })
  286. }
  287. })