welcome.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*******************************************************************************
  2. * Copyright(c) 2022 dongke All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:
  5. * 2.功能描述:欢迎页面画面
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 姜永辉 2022-11-22 1.00 新建
  9. *******************************************************************************/
  10. const app = getApp()
  11. const api = require('@/utils/api.js');
  12. const config = require('@/config/config.js');
  13. const Constants = require('@/utils/Constants.js');
  14. const util = require('@/utils/util.js');
  15. const { gradeCode } = require('@/utils/Constants.js');
  16. Page({
  17. /**
  18. * 页面的初始数据
  19. */
  20. data: {
  21. imageUrl: config.image_url + '/static/img/',
  22. },
  23. /**
  24. * 生命周期函数--监听页面加载
  25. */
  26. onLoad: function (options) {
  27. ("welcome", options);
  28. // options 中的 scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
  29. if (options.scene) {
  30. var scene = decodeURIComponent(options.scene)
  31. this.setData({
  32. scene: scene,
  33. })
  34. }
  35. // 从员工一览购买授权过来
  36. if (options.buy) {
  37. this.setData({
  38. buy: options.buy,
  39. })
  40. }
  41. // 小程序推送消息传过来的值
  42. if (options.loginType) {
  43. this.setData({
  44. loginType: options.loginType,
  45. //小程序推送消息传过来的值 pages/welcome/welcome?loginType=loginType&otherParam=值
  46. })
  47. }
  48. // 推送消息的其他参数-字符串处理,需要json到各自模块转换
  49. if (options.otherParam) {
  50. let reg = /\\/g
  51. let otherParam = options.otherParam.replace(reg, '')
  52. this.setData({
  53. otherParam
  54. })
  55. }
  56. // 从好有分享过来的数据 好友互利共赢 shareType=1 cpid userid startdate enddate
  57. if (options.shareType) {
  58. this.setData({
  59. shareType: options.shareType,
  60. })
  61. }
  62. // 来源公司id
  63. if (options.cpIdFrom) {
  64. this.setData({
  65. cpIdFrom: options.cpIdFrom,
  66. })
  67. }
  68. // 截止日期
  69. if (options.shareEndDate) {
  70. this.setData({
  71. shareEndDate: options.shareEndDate,
  72. })
  73. }
  74. // 来源公司 -userid
  75. if (options.userIdFrom) {
  76. this.setData({
  77. userIdFrom: options.userIdFrom,
  78. })
  79. }
  80. // 分享链接Id -shareId
  81. if (options.shareId) {
  82. this.setData({
  83. shareId: options.shareId,
  84. })
  85. }
  86. // 来源公司 -员工
  87. if (options.staffNameFrom) {
  88. this.setData({
  89. staffNameFrom: options.staffNameFrom,
  90. })
  91. }
  92. console.log(options,"welcome");
  93. },
  94. /**
  95. * 生命周期函数--监听页面显示
  96. */
  97. onShow: function () {
  98. // 登录 code
  99. this.wx_login()
  100. },
  101. /**
  102. * @desc : 登录
  103. * @date : 2022/5/12 13:49
  104. * @author : 姜永辉
  105. */
  106. wx_login() {
  107. let _this = this
  108. _this.wxlogin().then(res => {
  109. _this.getopenid({ code: res.code });
  110. })
  111. },
  112. /**
  113. * @desc : 获取微信派发的临时登录凭证
  114. * @date : 2022/5/12 13:49
  115. * @author : 姜永辉
  116. */
  117. wxlogin() {
  118. return new Promise((resolve, reject) => {
  119. // 登录
  120. wx.login({
  121. success: res => {
  122. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  123. console.log("wx.login", res);
  124. if (res.code) {
  125. resolve(res)
  126. } else {
  127. reject(res)
  128. }
  129. }
  130. })
  131. })
  132. },
  133. /**
  134. * @desc : 获取微信派发的 openid
  135. * @date : 2022/5/12 13:49
  136. * @author : 姜永辉
  137. */
  138. getopenid(param) {
  139. let _this = this
  140. api.request(Constants.SYS_OPENID_API + 'oauth/wx/login_wechat', 'POST', param).then(res => {
  141. if (res.data.code == Constants.SUCESS_CODE) {
  142. app.globalData.openid = res.data.data.openid
  143. app.globalData.unionId = res.data.data.unionid
  144. app.globalData.sessionkey = res.data.data.session_key
  145. _this.gettoken()
  146. }
  147. })
  148. },
  149. /**
  150. * @desc : 通过openid获取用户信息
  151. * @date : 2022/5/12 13:49
  152. * @author : 姜永辉
  153. */
  154. gettoken() {
  155. let _this = this
  156. // 调用接口
  157. _this.getTokenPromise().then(res => {
  158. let data = res.data.data;
  159. if (res.data.code === Constants.SUCESS_CODE) {
  160. // 记录登录的缓存
  161. app.globalData.company = data.company
  162. app.globalData.user = {
  163. userId: data.userId,
  164. userName: data.userName,
  165. userPhone: data.userPhone
  166. }
  167. // 说明注册未支付
  168. if (data.company && !data.company.license) {
  169. // 成功后跳转支付
  170. _this.toPay(res.data.data);
  171. // 调转选择公司
  172. if (this.data.shareType) {
  173. wx.reLaunch({
  174. url: '../../pages/login/login?shareType='
  175. + this.data.shareType + '&cpIdFrom=' + this.data.cpIdFrom
  176. + '&shareEndDate=' + this.data.shareEndDate
  177. + '&userIdFrom=' + this.data.userIdFrom
  178. + '&staffNameFrom=' + this.data.staffNameFrom
  179. + '&shareId=' + this.data.shareId,
  180. })
  181. } else {
  182. wx.reLaunch({
  183. url: '../../pages/login/login',
  184. })
  185. }
  186. return
  187. }
  188. app.globalData.token = res.data.data.accessToken
  189. // 读取后台设置的url
  190. if (data.company && data.company.svcIp && data.company.svcPort) {
  191. app.globalData.URI = data.company.svcIp + ':' + data.company.svcPort
  192. }
  193. if (data.company == undefined && data.joinedCps != undefined && data.joinedCps.length > 0) {
  194. // // 调转加入企业
  195. // wx.reLaunch({
  196. // url: '../../pages/company/company',
  197. // })
  198. // 调转选择公司
  199. if (this.data.shareType) {
  200. wx.reLaunch({
  201. url: '../../pages/login/login?shareType='
  202. + this.data.shareType + '&cpIdFrom=' + this.data.cpIdFrom
  203. + '&shareEndDate=' + this.data.shareEndDate
  204. + '&userIdFrom=' + this.data.userIdFrom
  205. + '&staffNameFrom=' + this.data.staffNameFrom
  206. + '&shareId=' + this.data.shareId,
  207. })
  208. } else {
  209. wx.reLaunch({
  210. url: '../../pages/login/login',
  211. })
  212. }
  213. }
  214. else
  215. if (data.company == undefined
  216. || res.data.data.company.cpId == null
  217. || res.data.data.company.cpId == ''
  218. || res.data.data.company.cpId == undefined) {
  219. // 调转选择公司
  220. if (this.data.shareType) {
  221. wx.reLaunch({
  222. url: '../../pages/login/login?shareType='
  223. + this.data.shareType + '&cpIdFrom=' + this.data.cpIdFrom
  224. + '&shareEndDate=' + this.data.shareEndDate
  225. + '&userIdFrom=' + this.data.userIdFrom
  226. + '&staffNameFrom=' + this.data.staffNameFrom
  227. + '&shareId=' + this.data.shareId,
  228. })
  229. } else {
  230. wx.reLaunch({
  231. url: '../../pages/login/login',
  232. })
  233. }
  234. }
  235. else {
  236. // // 接口登录后获取数据
  237. _this.getInfoAfterLogin()
  238. }
  239. }
  240. else if (res.data.code === 1015 || res.data.code === 1016) {
  241. if (res.data.code === 1016) {
  242. app.globalData.user = {
  243. userId: data.userId,
  244. userName: data.userName,
  245. userPhone: data.userPhone
  246. }
  247. app.globalData.openid = data.userWxid
  248. }
  249. // 当前用户未注册 // 公司到期
  250. // 调转选择公司
  251. setTimeout(() => {
  252. if (this.data.shareType) {
  253. wx.reLaunch({
  254. url: '../../pages/login/login?shareType='
  255. + this.data.shareType + '&cpIdFrom=' + this.data.cpIdFrom
  256. + '&shareEndDate=' + this.data.shareEndDate
  257. + '&userIdFrom=' + this.data.userIdFrom
  258. + '&staffNameFrom=' + this.data.staffNameFrom
  259. + '&shareId=' + this.data.shareId,
  260. })
  261. } else {
  262. wx.reLaunch({
  263. url: '../../pages/login/login',
  264. })
  265. }
  266. }, 2000)
  267. } else if (res.data.code == 1000) { // 令牌为空
  268. // 调转选择公司
  269. setTimeout(() => {
  270. if (this.data.shareType) {
  271. wx.reLaunch({
  272. url: '../../pages/login/login?shareType='
  273. + this.data.shareType + '&cpIdFrom=' + this.data.cpIdFrom
  274. + '&shareEndDate=' + this.data.shareEndDate
  275. + '&userIdFrom=' + this.data.userIdFrom
  276. + '&staffNameFrom=' + this.data.staffNameFrom
  277. + '&shareId=' + this.data.shareId,
  278. })
  279. } else {
  280. wx.reLaunch({
  281. url: '../../pages/login/login',
  282. })
  283. }
  284. }, 2000)
  285. }
  286. })
  287. },
  288. /**
  289. * @desc : 微信支付
  290. * @date : 2022/5/12 13:49
  291. * @author : 姜永辉
  292. */
  293. toPay(e) {
  294. let trade = e.company.trade
  295. let wechatPayService = app.globalData['wechatPayService']
  296. let params = {
  297. openId: app.globalData.openid,
  298. payFee: trade.tradeAmount,
  299. paymentSn: trade.tradeNo,
  300. }
  301. api.request(wechatPayService.prefix + wechatPayService.unifiedOrder, 'POST', params).then(res => {
  302. if (res.data.code === Constants.SUCESS_CODE) {
  303. var appid = res.data.data.appId
  304. var timeStamp = res.data.data.timeStamp
  305. var nonce_str = res.data.data.nonceStr
  306. var pk = res.data.data.pack
  307. var paySign = res.data.data.paySign
  308. let _this = this
  309. //调用微信支付
  310. wx.requestPayment({
  311. 'appId': appid,
  312. 'timeStamp': timeStamp,
  313. 'nonceStr': nonce_str,
  314. 'package': pk,
  315. 'signType': 'MD5',
  316. 'paySign': paySign,
  317. 'success': function (res) {
  318. wx.showToast({
  319. title: '支付成功',
  320. image: '/static/image/success.png',
  321. duration: 2000
  322. })
  323. _this.setData({
  324. loading: false
  325. })
  326. //刷新当前页面
  327. _this.onShow()
  328. },
  329. 'fail': function (res) {
  330. _this.setData({
  331. loading: false
  332. })
  333. },
  334. 'complete': function (res) {
  335. }
  336. });
  337. }
  338. });
  339. },
  340. /**
  341. * @desc : 登录后获取数据
  342. * @date : 2022/5/12 13:49
  343. * @author : 周兴
  344. */
  345. getInfoAfterLogin() {
  346. api.request('mdm-server/mst/staff/get_info_after_login', 'POST',
  347. {
  348. 'appCode': Constants.APP_CODE,
  349. 'cpId': app.globalData.company.cpId,
  350. 'userId': app.globalData.user.userId
  351. }).then(res => {
  352. if (res.data.code === Constants.SUCESS_CODE) {
  353. // 服务器的日期
  354. let nowDate = res.data.data.nowDate
  355. app.globalData.nowDate = nowDate
  356. // 员工
  357. let staff = res.data.data.staff
  358. if (staff && staff.staffId) {
  359. // 判断当前用户是否到期 标准版
  360. if (app.globalData.company.gradeCode == Constants.gradeCode.STD && !staff.flgInit) {
  361. let userEndDate = app.globalData.company.userEndDate
  362. if (userEndDate) {
  363. // 必须用服务器的返回时间来判断是否到期
  364. let diffs = new Date(userEndDate).dayDiff(new Date(nowDate))
  365. // 说明已经到期,跳转到
  366. if (diffs < 0) {
  367. util.showToast('用户已到期,到期日期【' + userEndDate + '】')
  368. setTimeout(() => {
  369. wx.reLaunch({
  370. url: '../../pages/login/login',
  371. })
  372. }, 2000)
  373. return;
  374. }
  375. }
  376. }
  377. app.globalData.user.staffId = staff.staffId
  378. app.globalData.user.staffCode = staff.staffCode
  379. app.globalData.user.staffName = staff.staffName
  380. app.globalData.user.staffPhone = staff.staffPhone
  381. app.globalData.user.roleIds = staff.roleIds
  382. app.globalData.user.orgId = staff.orgId
  383. app.globalData.user.orgName = staff.orgName
  384. app.globalData.user.defaultWhName = staff.defaultWhName
  385. app.globalData.user.defaultWhId = staff.defaultWhId
  386. app.globalData.user.flgBusiness = staff.flgBusiness
  387. app.globalData.user.flgInit = !!staff.flgInit
  388. // 1:PC端,2:移动端,3:PC+移动端, 4:无)
  389. if (staff.loginType == null || staff.loginType == 1 || staff.loginType == 4) {
  390. util.showToast('用户【' + staff.staffName + '】电话【' + staff.staffPhone + '】无权限')
  391. setTimeout(() => {
  392. wx.reLaunch({
  393. url: '../../pages/login/login',
  394. })
  395. }, 2000)
  396. return;
  397. }
  398. // 调转主页--注意有场景值的情况
  399. if (this.data.scene) {
  400. wx.reLaunch({
  401. url: '../../pages/home-page/home-page?scene=' + this.data.scene
  402. + '&loginType=' + this.data.loginType,
  403. })
  404. } else if (this.data.buy) {
  405. wx.reLaunch({
  406. url: '../../pages/home-page/home-page?buy=' + this.data.buy
  407. + '&loginType=' + this.data.loginType,
  408. })
  409. } else {
  410. wx.reLaunch({
  411. url: '../../pages/home-page/home-page?loginType='
  412. + this.data.loginType
  413. + (this.data.otherParam?'&otherParam=' + this.data.otherParam:''),
  414. })
  415. }
  416. }
  417. // 菜单
  418. let menuList = res.data.data.menuList
  419. if (menuList) {
  420. // 如果标准版把销售出库 修改为订单开单
  421. // if (app.globalData.company.gradeCode == Constants.gradeCode.STD) {
  422. // let filters = menuList.filter(it => it.objectCode == 'order-out');
  423. // if (filters && filters.length > 0) {
  424. // filters[0].menuName = '订单开单'
  425. // filters[0].name = '订单开单'
  426. // }
  427. // // 出库明细表修改为销售明细表
  428. // filters = menuList.filter(it => it.objectCode == 'out-detail-report');
  429. // if (filters && filters.length > 0) {
  430. // filters[0].menuName = '销售明细表'
  431. // filters[0].name = '销售明细表'
  432. // }
  433. // }
  434. // 处理操作链接
  435. let filters = menuList.filter(it => it.menuTags)
  436. if (filters && filters.length > 0) {
  437. filters.forEach(t => {
  438. if (t.menuTags.value) {
  439. let val = JSON.parse(t.menuTags.value)
  440. if (val.guideUrl == 'knowledge-view') {
  441. t.guideUrl = config.knowledge_url + '/' + val.guideUrl + '?appCode=CP-WXP&funId=' + t.funUuid
  442. }
  443. }
  444. })
  445. }
  446. // 处理功能包
  447. app.globalData.menus = util.handleMenu(app.globalData.company.funPackage, menuList);
  448. // console.log('menus',app.globalData.menus);
  449. }
  450. // 常用菜单
  451. let comMenuList = res.data.data.comMenuList
  452. if (comMenuList) {
  453. app.globalData.comMenuList = comMenuList
  454. }
  455. // 系统参数
  456. let setting = res.data.data.settingValue
  457. if (setting) {
  458. app.globalData.setting = JSON.parse(setting.setting.value)
  459. }
  460. // 系统字典
  461. let dataKindList = res.data.data.dataKindList
  462. if (dataKindList) {
  463. app.globalData.dataKindList = dataKindList
  464. }
  465. } else {
  466. // 报错跳转到登录界面
  467. setTimeout(() => {
  468. wx.reLaunch({
  469. url: '../../pages/login/login',
  470. })
  471. }, 3000)
  472. }
  473. })
  474. },
  475. /**
  476. * @desc : 获取微信派发的临时登录凭证
  477. * @date : 2022/5/12 13:49
  478. * @author : 姜永辉
  479. */
  480. getTokenPromise() {
  481. return new Promise((resolve, reject) => {
  482. api.request(Constants.SYS_OPENID_API + 'oauth/wx/token', 'POST',
  483. { 'userWxid': app.globalData.openid, unionId: app.globalData.unionId }).then(res => {
  484. resolve(res)
  485. })
  486. })
  487. },
  488. /**
  489. * @desc : 获取语言的方法
  490. * @author : 周兴
  491. * @date : 2024/1/19
  492. */
  493. $t(name) {
  494. if (this.data.$t[name]) {
  495. return this.data.$t[name];
  496. } else {
  497. return name;
  498. }
  499. },
  500. })