index.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. senceList:[
  70. {
  71. icon: 'vr@2x',
  72. url: '/pages/sence/sence',
  73. name: '场景列表',
  74. type: 31
  75. },
  76. {
  77. icon: 'vr_live@2x',
  78. url: '/pages/sence/sence',
  79. name: '带看',
  80. type: 32
  81. }
  82. ],
  83. relatedGoods: [],
  84. isLogin: app.globalData.isLogin
  85. },
  86. getGoodsRelated: function () {
  87. let that = this;
  88. util.request(api.GoodsRelated, {}).then(function (res) {
  89. if (res.errno === 0) {
  90. that.setData({
  91. relatedGoods: res.data.goodsList,
  92. });
  93. }
  94. });
  95. },
  96. tapByIdx:function(e){
  97. let {
  98. type
  99. } = e.currentTarget.dataset;
  100. let {
  101. url
  102. } = e.currentTarget.dataset;
  103. console.log(url+'?type='+type)
  104. // if (this.data.isLogin) {
  105. wx.navigateTo({
  106. url: url+'?type='+type,
  107. success: function (res) { },
  108. fail: function (res) { },
  109. complete: function (res) { },
  110. })
  111. // } else {
  112. // wx.navigateTo({
  113. // url: '/pages/auth/btnAuth/btnAuth',
  114. // })
  115. // // wx.showModal({
  116. // // title: '提示',
  117. // // content: '请登录后查看',
  118. // // showCancel: false,
  119. // // confirmColor: '#0075DC',
  120. // // success: res=>{
  121. // // }
  122. // // });
  123. // }
  124. },
  125. onLoad: function (options) {
  126. getApp().checkNetStatu();
  127. // 页面初始化 options为页面跳转所带来的参数
  128. // this.getGoodsRelated()
  129. // wx.setNavigationBarColor({
  130. // frontColor:'#ffffff',
  131. // backgroundColor:'#fb6400',
  132. // "navigationStyle":"custom"
  133. // animation: {
  134. // duration: 400,
  135. // timingFunc:'easeIn'
  136. // }
  137. // })
  138. },
  139. onReady: function () {
  140. },
  141. async getCounts() {
  142. let { data } = await util.request(api.OrderCount)
  143. if( data ){
  144. let orderList = this.data.orderList.map(item => {
  145. let ci = data.find(({code}) => code === item.id)
  146. if (ci) {
  147. item.count = ci.num > 99 ? '..' : ci.num
  148. }
  149. return { ...item }
  150. })
  151. this.setData({ orderList })
  152. }
  153. },
  154. async getUserInfo() {
  155. const {data} = await util.request(api.UserInfo)
  156. console.log('UserInfo',data)
  157. wx.setStorageSync('userinfoDetail', data)
  158. this.setData({
  159. userInfo: {
  160. nickName: data && data.nickname || '',
  161. avatarUrl:data && data.avatar || '',
  162. }
  163. })
  164. },
  165. async getBrandList () {
  166. let res = await util.request(api.GetAdminBrands)
  167. let list = res.data
  168. if (list && list.length>0) {
  169. this.setData({
  170. hasBrand: true
  171. })
  172. } else {
  173. this.setData({
  174. hasBrand: false
  175. })
  176. }
  177. },
  178. onShow: function () {
  179. getApp().updateCardCount()
  180. let userInfo = wx.getStorageSync('userInfo');
  181. let token = wx.getStorageSync('token');
  182. this.getBrandList()
  183. this.getUserInfo()
  184. this.getCounts()
  185. // 页面显示
  186. if (userInfo && token) {
  187. app.globalData.userInfo = userInfo;
  188. app.globalData.token = token;
  189. }
  190. this.setData({
  191. // userInfo: app.globalData.userInfo,
  192. token: app.globalData.token
  193. });
  194. util.request(api.CartList).then((res) => {
  195. let isLogin = false
  196. if (res.errno === 401) {
  197. isLogin = false
  198. app.globalData.userInfo = tempUserInfo;
  199. app.globalData.token = '';
  200. } else {
  201. isLogin = true
  202. app.globalData.userInfo = userInfo;
  203. app.globalData.token = token;
  204. }
  205. if (!isLogin){
  206. this.setData({
  207. token:'',
  208. userInfo: tempUserInfo
  209. });
  210. }
  211. this.setData({
  212. isLogin
  213. });
  214. });
  215. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  216. this.getTabBar().setData({
  217. selected: 3
  218. })
  219. }
  220. },
  221. onPullDownRefresh() {
  222. getApp().onPullDownRefresh()
  223. },
  224. onHide: function () {
  225. // 页面隐藏
  226. },
  227. onUnload: function () {
  228. // 页面关闭
  229. },
  230. bindGetUserInfo(e) {
  231. let userInfo = wx.getStorageSync('userInfo');
  232. let token = wx.getStorageSync('token');
  233. if (userInfo && token) {
  234. return;
  235. }
  236. if (e.detail.userInfo){
  237. //用户按了允许授权按钮
  238. user.loginByWeixin(e.detail).then(res => {
  239. this.setData({
  240. userInfo: res.data.userInfo,
  241. isLogin: true
  242. });
  243. res.data.userInfo.userId = res.data.userId
  244. res.data.userInfo.isAdmin = res.data.isAdmin
  245. wx.setStorageSync('userInfo', res.data.userInfo);
  246. wx.setStorageSync('token', res.data.token);
  247. wx.setStorageSync('userId', res.data.userId);
  248. app.globalData.userInfo = res.data.userInfo;
  249. app.globalData.token = res.data.token;
  250. }).catch((err) => {
  251. this.setData({
  252. token: '',
  253. userInfo: tempUserInfo
  254. });
  255. app.globalData.userInfo = tempUserInfo;
  256. app.globalData.token = '';
  257. console.log(err)
  258. });
  259. } else {
  260. //用户按了拒绝按钮
  261. // wx.showModal({
  262. // title: '温馨提示',
  263. // content: '您点击了"取消"授权,将无法获取杭州天门科技有限公司完整服务。请“授权”以保证各项功能正常使用。',
  264. // confirmText:'授权',
  265. // success: function (res) {
  266. // if (res.confirm) {
  267. // wx.openSetting({
  268. // success: (res) => {
  269. // if (res.authSetting["scope.userInfo"]) {////如果用户重新同意了授权登录
  270. // user.loginByWeixin(e.detail).then(res => {
  271. // this.setData({
  272. // userInfo: res.data.userInfo
  273. // });
  274. // app.globalData.userInfo = res.data.userInfo;
  275. // app.globalData.token = res.data.token;
  276. // }).catch((err) => {
  277. // console.log(err)
  278. // });
  279. // }
  280. // }
  281. // })
  282. // }
  283. // }
  284. // });
  285. }
  286. },
  287. exitLogin: function () {
  288. wx.showModal({
  289. title: '',
  290. confirmColor: '#b4282d',
  291. content: '退出登录?',
  292. success: function (res) {
  293. if (res.confirm) {
  294. wx.removeStorageSync('token');
  295. wx.removeStorageSync('userInfo');
  296. wx.switchTab({
  297. url: '/pages/index/index'
  298. });
  299. }
  300. }
  301. })
  302. },
  303. toReport () {
  304. wx.navigateTo({
  305. url: '/dataReport/pages/data-report/data-report',
  306. })
  307. }
  308. })