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. console.log(res.data)
  113. resolve(res.data)
  114. })
  115. },
  116. fail: function(err) {
  117. reject(err.code)
  118. }
  119. })
  120. })
  121. return promise
  122. }
  123. //获取手机号
  124. const getPhone = (e, userInfo) => {
  125. let promise = new Promise(function(resolve, reject) {
  126. if (e.mp.detail.errMsg === "getPhoneNumber:ok") {
  127. let appId = 'wx5d8906c2208c899f'
  128. let sessionKey = userInfo.sessionKey
  129. let encryptedData = e.mp.detail.encryptedData
  130. let iv = e.mp.detail.iv
  131. let data =''
  132. try {
  133. console.log('解密前 encryptedData: ', encryptedData)
  134. console.log('解密前 iv: ', iv)
  135. console.log('解密前 appId: ', appId)
  136. console.log('解密前 sessionKey: ', sessionKey)
  137. data= WXBizDataCrypt.prototype.decryptData(encryptedData, iv, appId, sessionKey)
  138. console.log('解密后 data: ', data)
  139. resolve(data.phoneNumber)
  140. } catch (e) {
  141. uni.showToast({
  142. icon: "none",
  143. title: e,
  144. duration: 3000
  145. });
  146. }
  147. }
  148. })
  149. return promise
  150. }
  151. //同步信息
  152. const syncInfo = (userInfo) => {
  153. let promise = new Promise(function(resolve, reject) {
  154. if (!userInfo.head) userInfo.head =
  155. 'https://taohaoliang.oss-cn-beijing.aliyuncs.com/app/card_head.png'
  156. if (!userInfo.nickname) {
  157. let code = ""
  158. for (var i = 0; i < 6; i++) {
  159. code += parseInt(Math.random() * 10);
  160. }
  161. userInfo.nickname = 'user' + code
  162. }
  163. baseRequest('commonUserApp', 'edit', {
  164. commonUserInfo: JSON.stringify(userInfo)
  165. }, failres => {
  166. uni.showToast({
  167. icon: "none",
  168. title: failres.errmsg,
  169. duration: 3000
  170. });
  171. reject(err.code)
  172. }).then(res => {
  173. resolve(res.data)
  174. })
  175. })
  176. return promise
  177. }
  178. // 处理海报数据
  179. const makeCanvasData = (check_idx,height,posterObj1,theight) => {
  180. console.log(height,theight)
  181. var dth = (theight - 22) * 2
  182. var h=((height+40)*2)
  183. console.log(posterObj1)
  184. var posterObj= {}
  185. switch (check_idx) {
  186. case 0:
  187. posterObj={
  188. width: '670rpx',
  189. height: h+'rpx',
  190. background: '#fff',
  191. borderRadius: '16rpx',
  192. views:[
  193. {
  194. type: 'text',
  195. text: posterObj1.title,
  196. use:'title',
  197. css: {
  198. fontSize: '32rpx',
  199. color: '#1A2033',
  200. fontWeight: 'bold',
  201. lineHeight: '45rpx',
  202. left: '32rpx',
  203. top: '410rpx',
  204. width: '606rpx'
  205. }
  206. },
  207. {
  208. type: 'text',
  209. text: posterObj1.title1,
  210. use:'title1',
  211. css: {
  212. fontSize: '28rpx',
  213. fontWeight: 'bold',
  214. color: '#1A2033',
  215. lineHeight: '28rpx',
  216. left: '32rpx',
  217. top: 535+dth+'rpx'
  218. }
  219. },
  220. {
  221. type: 'text',
  222. text: '长按或扫描识别二维码',
  223. use:'title2',
  224. css: {
  225. fontSize: '24rpx',
  226. color: '#4070FF',
  227. lineHeight: '24rpx',
  228. left: '32rpx',
  229. top: 579+dth+'rpx'
  230. }
  231. },
  232. {
  233. type: 'image',
  234. src: posterObj1.image,
  235. use:'dt',
  236. css: {
  237. left: '32rpx',
  238. top: '44rpx',
  239. width: '606rpx',
  240. height: '341rpx',
  241. borderRadius: '16rpx'
  242. }
  243. },
  244. {
  245. type: 'image',
  246. src: posterObj1.qrcode,
  247. use:'qrcode',
  248. css: {
  249. left: '455rpx',
  250. top: 520+dth+'rpx',
  251. // transform: 'translate(-50%,0)',
  252. width: '240rpx',
  253. height: '240rpx',
  254. }
  255. },
  256. // {
  257. // type: 'text',
  258. // text: '———— 由云现场提供技术支持 ————',
  259. // css: {
  260. // left: '0',
  261. // top: '863rpx',
  262. // width: '100%',
  263. // textAlign: 'center',
  264. // fontSize: '24rpx',
  265. // color: '#989FB3',
  266. // lineHeight: '33rpx'
  267. // }
  268. // }
  269. ],
  270. }
  271. break;
  272. case 1:
  273. posterObj={
  274. width: '670rpx',
  275. height: h+'rpx',
  276. background: '#fff',
  277. borderRadius: '16rpx',
  278. views: [{
  279. type: 'image',
  280. use:'bg',
  281. src: 'https://s.yun-live.com/images/20210201/39ae4d9d8ad0b1acac7c224e845c641f.png',
  282. css: {
  283. left: '0',
  284. top: '0',
  285. width: '100%',
  286. height: '100%'
  287. }
  288. },
  289. {
  290. type: 'image',
  291. src: posterObj1.image,
  292. css: {
  293. left: '32rpx',
  294. top: '44rpx',
  295. width: '606rpx',
  296. height: '341rpx',
  297. borderRadius: '16rpx'
  298. }
  299. },
  300. {
  301. type: 'text',
  302. text: posterObj1.title,
  303. use:'title',
  304. css: {
  305. fontSize: '32rpx',
  306. color: '#fff',
  307. fontWeight: 'bold',
  308. lineHeight: '45rpx',
  309. left: '32rpx',
  310. top: '420rpx',
  311. width: '590rpx'
  312. }
  313. },
  314. {
  315. type: 'text',
  316. text:posterObj1.title1,
  317. use:'title1',
  318. css: {
  319. fontSize: '28rpx',
  320. fontWeight: 'bold',
  321. color: '#D8AB87',
  322. lineHeight: '28rpx',
  323. left: '32rpx',
  324. top: 535+dth+'rpx'
  325. }
  326. },
  327. {
  328. type: 'text',
  329. text: '长按或扫描识别二维码',
  330. use:'title2',
  331. css: {
  332. fontSize: '24rpx',
  333. color: '#FFFFFF',
  334. lineHeight: '24rpx',
  335. left: '32rpx',
  336. top: 579+dth+'rpx'
  337. }
  338. },
  339. {
  340. type: 'image',
  341. src: 'https://s.yun-live.com/images/20210201/d88d56843d43b917e2a28550b2a62723.png',
  342. css: {
  343. left: '0rpx',
  344. top: '90%',
  345. width: '103rpx',
  346. height: '103rpx',
  347. }
  348. },
  349. {
  350. type: 'image',
  351. src: 'https://s.yun-live.com/images/20210201/247736ffd279276b891ec14db8ed0fd0.png',
  352. css: {
  353. left: '600rpx',
  354. top: '50%',
  355. width: '56.4rpx',
  356. height: '56.4rpx',
  357. }
  358. },
  359. {
  360. type: 'view',
  361. use:'qrcode-view',
  362. css: {
  363. left: '445rpx',
  364. top: 520+dth+'rpx',
  365. width: '240rpx',
  366. height: '240rpx',
  367. background: '#fff',
  368. }
  369. },
  370. {
  371. type: 'image',
  372. src: posterObj1.qrcode,
  373. use:'qrcode',
  374. css: {
  375. left: '455rpx',
  376. top: 530+dth+'rpx',
  377. // transform: 'translate(-50%,0)',
  378. width: '220rpx',
  379. height: '220rpx',
  380. }
  381. },
  382. ]
  383. }
  384. break;
  385. case 2:
  386. posterObj={
  387. width: '670rpx',
  388. height: h+'rpx',
  389. background: '#fff',
  390. borderRadius: '16rpx',
  391. views: [{
  392. type: 'image',
  393. use:'bg',
  394. src: 'https://s.yun-live.com/images/20210201/78f227bd701da20676c9da9166ce3144.png',
  395. css: {
  396. left: '0',
  397. top: '0',
  398. width: '100%',
  399. height: '100%'
  400. }
  401. },
  402. {
  403. type: 'image',
  404. src: posterObj1.image,
  405. css: {
  406. left: '62rpx',
  407. top: '84rpx',
  408. width: '540rpx',
  409. height: '304rpx',
  410. borderRadius: '16rpx'
  411. }
  412. },
  413. {
  414. type: 'text',
  415. text: posterObj1.title,
  416. use:'title',
  417. css: {
  418. fontSize: '32rpx',
  419. color: '#1D1D25',
  420. fontWeight: 'bold',
  421. lineHeight: '45rpx',
  422. left: '62rpx',
  423. top: '420rpx',
  424. width: '530rpx'
  425. }
  426. },
  427. {
  428. type: 'text',
  429. text:posterObj1.title1,
  430. use:'title1',
  431. css: {
  432. fontSize: '28rpx',
  433. fontWeight: 'bold',
  434. color: '#1D1D25',
  435. lineHeight: '28rpx',
  436. left: '62rpx',
  437. top: 515+dth+'rpx'
  438. }
  439. },
  440. {
  441. type: 'text',
  442. text: '长按或扫描识别二维码',
  443. use:'title2',
  444. css: {
  445. fontSize: '24rpx',
  446. color: '#6CB37A',
  447. lineHeight: '24rpx',
  448. left: '62rpx',
  449. top: 559+dth+'rpx'
  450. }
  451. },
  452. {
  453. type: 'view',
  454. use:'qrcode-view',
  455. css: {
  456. left: '405rpx',
  457. top: 500+dth+'rpx',
  458. width: '240rpx',
  459. height: '240rpx',
  460. background: '#fff',
  461. }
  462. },
  463. {
  464. type: 'image',
  465. src: posterObj1.qrcode,
  466. use:'qrcode',
  467. css: {
  468. left: '415rpx',
  469. top: 510+dth+'rpx',
  470. // transform: 'translate(-50%,0)',
  471. width: '220rpx',
  472. height: '220rpx',
  473. }
  474. },]
  475. }
  476. break;
  477. case 3:
  478. posterObj={
  479. width: '670rpx',
  480. height: h+'rpx',
  481. background: '#fff',
  482. borderRadius: '16rpx',
  483. views: [{
  484. type: 'image',
  485. use:'bg',
  486. src: 'https://s.yun-live.com/images/20210201/524ab6a41fe8c7eb57b35df9a547d388.png',
  487. css: {
  488. left: '0',
  489. top: '0',
  490. width: '100%',
  491. height: '100%'
  492. }
  493. },
  494. {
  495. type: 'image',
  496. src: posterObj1.image,
  497. css: {
  498. left: '32rpx',
  499. top: '44rpx',
  500. width: '606rpx',
  501. height: '341rpx',
  502. borderRadius: '16rpx'
  503. }
  504. },
  505. {
  506. type: 'text',
  507. text: posterObj1.title,
  508. use:'title',
  509. css: {
  510. fontSize: '32rpx',
  511. color: '#1D1D25',
  512. fontWeight: 'bold',
  513. lineHeight: '45rpx',
  514. left: '32rpx',
  515. top: '410rpx',
  516. width: '530rpx'
  517. }
  518. },
  519. {
  520. type: 'text',
  521. text:posterObj1.title1,
  522. use:'title1',
  523. css: {
  524. fontSize: '28rpx',
  525. fontWeight: 'bold',
  526. color: '#1D1D25',
  527. lineHeight: '28rpx',
  528. left: '32rpx',
  529. top: 525+dth+'rpx'
  530. }
  531. },
  532. {
  533. type: 'text',
  534. text: '长按或扫描识别二维码',
  535. use:'title2',
  536. css: {
  537. fontSize: '24rpx',
  538. color: '#6CB37A',
  539. lineHeight: '24rpx',
  540. left: '32rpx',
  541. top: 569+dth+'rpx'
  542. }
  543. },
  544. {
  545. type: 'view',
  546. use:'qrcode-view',
  547. css: {
  548. left: '440rpx',
  549. top: 500+dth+'rpx',
  550. width: '240rpx',
  551. height: '240rpx',
  552. background: '#fff',
  553. }
  554. },
  555. {
  556. type: 'image',
  557. src: posterObj1.qrcode,
  558. use:'qrcode',
  559. css: {
  560. left: '450rpx',
  561. top: 510+dth+'rpx',
  562. // transform: 'translate(-50%,0)',
  563. width: '220rpx',
  564. height: '220rpx',
  565. }
  566. },]
  567. }
  568. // this.posterObj1.backgroundType='image'
  569. // this.posterObj1.background='https://s.yun-live.com/images/20210201/524ab6a41fe8c7eb57b35df9a547d388.png';
  570. // this.posterObj1.width='606rpx',
  571. // this.posterObj1.height='341rpx',
  572. // this.posterObj1.titleCssColor='#1D1D25'
  573. // this.posterObj1.title1CssColor='#1D1D25'
  574. // this.posterObj1.title2CssColor='#6CB37A'
  575. // break;
  576. default:
  577. break;
  578. }
  579. return posterObj
  580. }
  581. export default {
  582. baseUrl,
  583. baseRequest,
  584. TokenRequest,
  585. wxlogin,
  586. getPhone,
  587. syncInfo,
  588. makeCanvasData
  589. }