request.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. import * as config from '@/config'
  2. import WXBizDataCrypt from './WXBizDataCrypt.js'
  3. let baseUrl = config.def().baseUrlNew
  4. // 定义基础请求路径(后端服务器地址)
  5. const baseRequest = (_gp, _mt, data = {}, failCallback) => {
  6. //异步请求数据
  7. return new Promise(resolve => {
  8. // if (!userInfo || !userInfo.accessToken) {
  9. // userInfo = uni.getStorageSync('userInfo')
  10. // }
  11. // let accessToken = userInfo ? userInfo.accessToken : ''
  12. let baseUrl = config.def().baseUrlNew
  13. uni.request({
  14. url: baseUrl + '/m.api',
  15. data: {
  16. ...data,
  17. _gp,
  18. _mt
  19. },
  20. method: 'POST',
  21. header: {
  22. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  23. // 'ACCESSTOKEN': accessToken
  24. },
  25. success: (res) => {
  26. if (res.statusCode === 200) {
  27. if (res.data.errno === 200) {
  28. resolve(res.data);
  29. } else {
  30. if (failCallback) {
  31. failCallback(res.data)
  32. } else {
  33. uni.showToast({
  34. title: res.data.errmsg,
  35. icon: 'none'
  36. })
  37. }
  38. }
  39. }
  40. }
  41. })
  42. })
  43. }
  44. //带Token请求
  45. const TokenRequest = (method, url, data, header) => {
  46. var contentheader = 'application/json'
  47. if (header) {
  48. contentheader = header
  49. }
  50. let ac_token = "";
  51. uni.getStorage({
  52. key: 'userInfo',
  53. success: function(res) {
  54. ac_token = res.data.accessToken
  55. }
  56. });
  57. //此token是登录成功后后台返回保存在storage中的
  58. let DefaultOpts = {
  59. url: baseUrl + url,
  60. data: data,
  61. method: method,
  62. header: {
  63. 'content-type': contentheader,
  64. 'Token': ac_token,
  65. }
  66. }
  67. let promise = new Promise(function(resolve, reject) {
  68. uni.request(DefaultOpts).then(
  69. (res) => {
  70. console.log(JSON.stringify(res[1].data))
  71. if (res[1].data.code == '200' || res[1].data.code == 200) {
  72. // 后端返回的状态码100为成功状态,成功则返回请求结果,在app调试时可以通过console.log(JSON.stringify(res[1].data))来查看返回值(以项目实际情况为准)
  73. resolve(res[1].data)
  74. }
  75. if (res[1].data.code == '105' || res[1].data.code == 105) {
  76. // 后端返回状态码为105则为未登录状态(以项目实际情况为准)
  77. uni.showToast({
  78. icon: 'none',
  79. title: '尚未登录',
  80. duration: 2000
  81. });
  82. // 尚未登录的逻辑处理
  83. return false
  84. }
  85. }
  86. ).catch(
  87. (response) => {
  88. reject(response)
  89. }
  90. )
  91. })
  92. return promise
  93. }
  94. //微信登录
  95. const wxlogin = () => {
  96. let promise = new Promise(function(resolve, reject) {
  97. uni.login({
  98. "provider": "weixin",
  99. success: function(wxres) {
  100. console.log("wxlogin",wxres)
  101. baseRequest('commonUserApp', 'commonUserLogin', {
  102. loginType: 1,
  103. raw: JSON.stringify(wxres)
  104. }, failres => {
  105. uni.showToast({
  106. icon: "none",
  107. title: failres.errmsg,
  108. duration: 3000
  109. });
  110. uni.hideLoading()
  111. }).then(res => {
  112. resolve(res.data)
  113. })
  114. },
  115. fail: function(err) {
  116. reject(err.code)
  117. }
  118. })
  119. })
  120. return promise
  121. }
  122. //获取手机号
  123. const getPhone = (e, userInfo) => {
  124. let promise = new Promise(function(resolve, reject) {
  125. if (e.mp.detail.errMsg === "getPhoneNumber:ok") {
  126. let appId = 'wx5d8906c2208c899f'
  127. let sessionKey = userInfo.sessionKey
  128. let encryptedData = e.mp.detail.encryptedData
  129. let iv = e.mp.detail.iv
  130. let data =''
  131. try {
  132. console.log('解密前 encryptedData: ', encryptedData)
  133. console.log('解密前 iv: ', iv)
  134. console.log('解密前 appId: ', appId)
  135. console.log('解密前 sessionKey: ', sessionKey)
  136. data= WXBizDataCrypt.prototype.decryptData(encryptedData, iv, appId, sessionKey)
  137. console.log('解密后 data: ', data)
  138. resolve(data.phoneNumber)
  139. } catch (e) {
  140. uni.showToast({
  141. icon: "none",
  142. title: e,
  143. duration: 3000
  144. });
  145. }
  146. }
  147. })
  148. return promise
  149. }
  150. //同步信息
  151. const syncInfo = (userInfo) => {
  152. let promise = new Promise(function(resolve, reject) {
  153. if (!userInfo.head) userInfo.head =
  154. 'https://taohaoliang.oss-cn-beijing.aliyuncs.com/app/card_head.png'
  155. if (!userInfo.nickname) {
  156. let code = ""
  157. for (var i = 0; i < 6; i++) {
  158. code += parseInt(Math.random() * 10);
  159. }
  160. userInfo.nickname = 'user' + code
  161. }
  162. baseRequest('commonUserApp', 'edit', {
  163. commonUserInfo: JSON.stringify(userInfo)
  164. }, failres => {
  165. uni.showToast({
  166. icon: "none",
  167. title: failres.errmsg,
  168. duration: 3000
  169. });
  170. reject(err.code)
  171. }).then(res => {
  172. resolve(res.data)
  173. })
  174. })
  175. return promise
  176. }
  177. // 处理海报数据
  178. const makeCanvasData = (check_idx,height,posterObj1,theight) => {
  179. console.log(height,theight)
  180. var dth = (theight - 22) * 2
  181. var h=((height+40)*2)
  182. console.log(posterObj1)
  183. var posterObj= {}
  184. switch (check_idx) {
  185. case 0:
  186. posterObj={
  187. width: '670rpx',
  188. height: h+'rpx',
  189. background: '#fff',
  190. borderRadius: '16rpx',
  191. views:[
  192. {
  193. type: 'text',
  194. text: posterObj1.title,
  195. use:'title',
  196. css: {
  197. fontSize: '32rpx',
  198. color: '#1A2033',
  199. fontWeight: 'bold',
  200. lineHeight: '45rpx',
  201. left: '32rpx',
  202. top: '410rpx',
  203. width: '606rpx'
  204. }
  205. },
  206. {
  207. type: 'text',
  208. text: posterObj1.title1,
  209. use:'title1',
  210. css: {
  211. fontSize: '28rpx',
  212. fontWeight: 'bold',
  213. color: '#1A2033',
  214. lineHeight: '28rpx',
  215. left: '32rpx',
  216. top: 535+dth+'rpx'
  217. }
  218. },
  219. {
  220. type: 'text',
  221. text: '扫描识别二维码',
  222. use:'title2',
  223. css: {
  224. fontSize: '24rpx',
  225. color: '#4070FF',
  226. lineHeight: '24rpx',
  227. left: '32rpx',
  228. top: 579+dth+'rpx'
  229. }
  230. },
  231. {
  232. type: 'image',
  233. src: posterObj1.image,
  234. use:'dt',
  235. css: {
  236. left: '32rpx',
  237. top: '44rpx',
  238. width: '606rpx',
  239. height: '341rpx',
  240. borderRadius: '16rpx'
  241. }
  242. },
  243. {
  244. type: 'image',
  245. src: posterObj1.qrcode,
  246. use:'qrcode',
  247. css: {
  248. left: '410rpx',
  249. top: 520+dth+'rpx',
  250. // transform: 'translate(-50%,0)',
  251. width: '240rpx',
  252. height: '240rpx',
  253. }
  254. },
  255. // {
  256. // type: 'text',
  257. // text: '———— 由云现场提供技术支持 ————',
  258. // css: {
  259. // left: '0',
  260. // top: '863rpx',
  261. // width: '100%',
  262. // textAlign: 'center',
  263. // fontSize: '24rpx',
  264. // color: '#989FB3',
  265. // lineHeight: '33rpx'
  266. // }
  267. // }
  268. ],
  269. }
  270. break;
  271. case 1:
  272. posterObj={
  273. width: '670rpx',
  274. height: h+'rpx',
  275. background: '#fff',
  276. borderRadius: '16rpx',
  277. views: [{
  278. type: 'image',
  279. use:'bg',
  280. src: 'https://s.yun-live.com/images/20210201/39ae4d9d8ad0b1acac7c224e845c641f.png',
  281. css: {
  282. left: '0',
  283. top: '0',
  284. width: '100%',
  285. height: '100%'
  286. }
  287. },
  288. {
  289. type: 'image',
  290. src: posterObj1.image,
  291. css: {
  292. left: '32rpx',
  293. top: '44rpx',
  294. width: '606rpx',
  295. height: '341rpx',
  296. borderRadius: '16rpx'
  297. }
  298. },
  299. {
  300. type: 'text',
  301. text: posterObj1.title,
  302. use:'title',
  303. css: {
  304. fontSize: '32rpx',
  305. color: '#fff',
  306. fontWeight: 'bold',
  307. lineHeight: '45rpx',
  308. left: '32rpx',
  309. top: '420rpx',
  310. width: '590rpx'
  311. }
  312. },
  313. {
  314. type: 'text',
  315. text:posterObj1.title1,
  316. use:'title1',
  317. css: {
  318. fontSize: '28rpx',
  319. fontWeight: 'bold',
  320. color: '#D8AB87',
  321. lineHeight: '28rpx',
  322. left: '32rpx',
  323. top: 535+dth+'rpx'
  324. }
  325. },
  326. {
  327. type: 'text',
  328. text: '扫描识别二维码',
  329. use:'title2',
  330. css: {
  331. fontSize: '24rpx',
  332. color: '#FFFFFF',
  333. lineHeight: '24rpx',
  334. left: '32rpx',
  335. top: 579+dth+'rpx'
  336. }
  337. },
  338. {
  339. type: 'image',
  340. src: 'https://s.yun-live.com/images/20210201/d88d56843d43b917e2a28550b2a62723.png',
  341. css: {
  342. left: '0rpx',
  343. top: '90%',
  344. width: '103rpx',
  345. height: '103rpx',
  346. }
  347. },
  348. {
  349. type: 'image',
  350. src: 'https://s.yun-live.com/images/20210201/247736ffd279276b891ec14db8ed0fd0.png',
  351. css: {
  352. left: '600rpx',
  353. top: '50%',
  354. width: '56.4rpx',
  355. height: '56.4rpx',
  356. }
  357. },
  358. {
  359. type: 'view',
  360. use:'qrcode-view',
  361. css: {
  362. left: '400rpx',
  363. top: 520+dth+'rpx',
  364. width: '240rpx',
  365. height: '240rpx',
  366. background: '#fff',
  367. }
  368. },
  369. {
  370. type: 'image',
  371. src: posterObj1.qrcode,
  372. use:'qrcode',
  373. css: {
  374. left: '410rpx',
  375. top: 530+dth+'rpx',
  376. // transform: 'translate(-50%,0)',
  377. width: '220rpx',
  378. height: '220rpx',
  379. }
  380. },
  381. ]
  382. }
  383. break;
  384. case 2:
  385. posterObj={
  386. width: '670rpx',
  387. height: h+'rpx',
  388. background: '#fff',
  389. borderRadius: '16rpx',
  390. views: [{
  391. type: 'image',
  392. use:'bg',
  393. src: 'https://s.yun-live.com/images/20210201/78f227bd701da20676c9da9166ce3144.png',
  394. css: {
  395. left: '0',
  396. top: '0',
  397. width: '100%',
  398. height: '100%'
  399. }
  400. },
  401. {
  402. type: 'image',
  403. src: posterObj1.image,
  404. css: {
  405. left: '62rpx',
  406. top: '84rpx',
  407. width: '540rpx',
  408. height: '304rpx',
  409. borderRadius: '16rpx'
  410. }
  411. },
  412. {
  413. type: 'text',
  414. text: posterObj1.title,
  415. use:'title',
  416. css: {
  417. fontSize: '32rpx',
  418. color: '#1D1D25',
  419. fontWeight: 'bold',
  420. lineHeight: '45rpx',
  421. left: '62rpx',
  422. top: '420rpx',
  423. width: '530rpx'
  424. }
  425. },
  426. {
  427. type: 'text',
  428. text:posterObj1.title1,
  429. use:'title1',
  430. css: {
  431. fontSize: '28rpx',
  432. fontWeight: 'bold',
  433. color: '#1D1D25',
  434. lineHeight: '28rpx',
  435. left: '62rpx',
  436. top: 515+dth+'rpx'
  437. }
  438. },
  439. {
  440. type: 'text',
  441. text: '扫描识别二维码',
  442. use:'title2',
  443. css: {
  444. fontSize: '24rpx',
  445. color: '#6CB37A',
  446. lineHeight: '24rpx',
  447. left: '62rpx',
  448. top: 559+dth+'rpx'
  449. }
  450. },
  451. {
  452. type: 'view',
  453. use:'qrcode-view',
  454. css: {
  455. left: '365rpx',
  456. top: 500+dth+'rpx',
  457. width: '240rpx',
  458. height: '240rpx',
  459. background: '#fff',
  460. }
  461. },
  462. {
  463. type: 'image',
  464. src: posterObj1.qrcode,
  465. use:'qrcode',
  466. css: {
  467. left: '375rpx',
  468. top: 510+dth+'rpx',
  469. // transform: 'translate(-50%,0)',
  470. width: '220rpx',
  471. height: '220rpx',
  472. }
  473. },]
  474. }
  475. break;
  476. case 3:
  477. posterObj={
  478. width: '670rpx',
  479. height: h+'rpx',
  480. background: '#fff',
  481. borderRadius: '16rpx',
  482. views: [{
  483. type: 'image',
  484. use:'bg',
  485. src: 'https://s.yun-live.com/images/20210201/524ab6a41fe8c7eb57b35df9a547d388.png',
  486. css: {
  487. left: '0',
  488. top: '0',
  489. width: '100%',
  490. height: '100%'
  491. }
  492. },
  493. {
  494. type: 'image',
  495. src: posterObj1.image,
  496. css: {
  497. left: '32rpx',
  498. top: '44rpx',
  499. width: '606rpx',
  500. height: '341rpx',
  501. borderRadius: '16rpx'
  502. }
  503. },
  504. {
  505. type: 'text',
  506. text: posterObj1.title,
  507. use:'title',
  508. css: {
  509. fontSize: '32rpx',
  510. color: '#1D1D25',
  511. fontWeight: 'bold',
  512. lineHeight: '45rpx',
  513. left: '32rpx',
  514. top: '410rpx',
  515. width: '530rpx'
  516. }
  517. },
  518. {
  519. type: 'text',
  520. text:posterObj1.title1,
  521. use:'title1',
  522. css: {
  523. fontSize: '28rpx',
  524. fontWeight: 'bold',
  525. color: '#1D1D25',
  526. lineHeight: '28rpx',
  527. left: '32rpx',
  528. top: 525+dth+'rpx'
  529. }
  530. },
  531. {
  532. type: 'text',
  533. text: '扫描识别二维码',
  534. use:'title2',
  535. css: {
  536. fontSize: '24rpx',
  537. color: '#6CB37A',
  538. lineHeight: '24rpx',
  539. left: '32rpx',
  540. top: 569+dth+'rpx'
  541. }
  542. },
  543. {
  544. type: 'view',
  545. use:'qrcode-view',
  546. css: {
  547. left: '400rpx',
  548. top: 500+dth+'rpx',
  549. width: '240rpx',
  550. height: '240rpx',
  551. background: '#fff',
  552. }
  553. },
  554. {
  555. type: 'image',
  556. src: posterObj1.qrcode,
  557. use:'qrcode',
  558. css: {
  559. left: '410rpx',
  560. top: 510+dth+'rpx',
  561. // transform: 'translate(-50%,0)',
  562. width: '220rpx',
  563. height: '220rpx',
  564. }
  565. },]
  566. }
  567. // this.posterObj1.backgroundType='image'
  568. // this.posterObj1.background='https://s.yun-live.com/images/20210201/524ab6a41fe8c7eb57b35df9a547d388.png';
  569. // this.posterObj1.width='606rpx',
  570. // this.posterObj1.height='341rpx',
  571. // this.posterObj1.titleCssColor='#1D1D25'
  572. // this.posterObj1.title1CssColor='#1D1D25'
  573. // this.posterObj1.title2CssColor='#6CB37A'
  574. // break;
  575. default:
  576. break;
  577. }
  578. return posterObj
  579. }
  580. export default {
  581. baseUrl,
  582. baseRequest,
  583. TokenRequest,
  584. wxlogin,
  585. getPhone,
  586. syncInfo,
  587. makeCanvasData
  588. }