拓恒飞手平台小程序
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // package_B/page/changeMessage/index.js
  2. import {changeMessage} from '../../../api/mine.js'
  3. import {getOssAuth} from '../../../api/upload.js'
  4. import {Base64} from 'js-base64'
  5. // const Base64 = require('js-base64')
  6. const crypto = require('crypto-js')
  7. import {checkPhoneNumber, checkUserName} from '../../../utils/check.js'
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. userInfo: {},
  14. ossForm: {},
  15. form: {},
  16. formRules: {
  17. avatar: {
  18. type: 'image',
  19. title: "用户头像",
  20. tips: "请输入用户头像",
  21. validator: function (value) {
  22. return value != ''
  23. },
  24. placeholder: "请输入用户头像"
  25. },
  26. nickname: {
  27. type: 'input',
  28. title: "用户昵称",
  29. tips: "请输入用户昵称(中文、数字、字母)",
  30. validator: function (value) {
  31. return value != "" && checkUserName(value)
  32. },
  33. placeholder: "请输入用户昵称(中文、数字、字母)"
  34. },
  35. mobile: {
  36. type: 'input',
  37. title: "电话号码",
  38. tips: "请输入正确格式的电话号码",
  39. validator: function (value) {
  40. return value != '' && checkPhoneNumber(value)
  41. },
  42. placeholder: "请输入正确格式的电话号码"
  43. }
  44. }
  45. },
  46. onLoad(e) {
  47. let userInfo = wx.getStorageSync('userInfo')
  48. let form = this.data.form
  49. form = Object.assign({}, userInfo)
  50. this.setData({form, userInfo})
  51. },
  52. onShow() {
  53. this.getAuth()
  54. },
  55. bindValue(e) {
  56. let name = e.currentTarget.dataset.name;
  57. let str = "form."+name
  58. this.setData({
  59. [str]: e.detail.value,
  60. })
  61. this.validate(name, e.detail.value)
  62. },
  63. validate(name) {
  64. let formRules = this.data.formRules;
  65. let validator = this.data.formRules[name].validator
  66. let result = false
  67. if(!this.data.form[name]) {
  68. result = true
  69. } else {
  70. result = validator ? !validator(this.data.form[name]) : false;
  71. }
  72. formRules[name].warning = result
  73. this.setData({
  74. formRules
  75. })
  76. return result
  77. },
  78. validateForm() {
  79. return new Promise((resolve, reject) => {
  80. try {
  81. let formRules = this.data.formRules;
  82. let result = false;
  83. for (let key in formRules) {
  84. let temp = this.validate(key)
  85. if (temp) {
  86. result = temp
  87. }
  88. }
  89. resolve(!result)
  90. } catch (e) {
  91. reject(e)
  92. }
  93. })
  94. },
  95. /* 上传图片 */
  96. uploadImage(){
  97. wx.chooseImage({
  98. count: 1, // 最多可以选择的图片张数,默认9
  99. mediaType: ['image'], // 图片
  100. sizeType: ['original'], // original 原图,compressed 压缩图,默认二者都有
  101. sourceType: ['album', 'camera'], // album 从相册选图,camera 使用相机,默认二者都有
  102. success:(res) =>{
  103. // success
  104. let form = this.data.form
  105. form.avatar = res.tempFilePaths
  106. this.setData({ form })
  107. this.validate('avatar')
  108. },
  109. fail: (e) => {
  110. console.log(e);
  111. }
  112. })
  113. },
  114. getAuth() {
  115. let that = this
  116. getOssAuth().then(res=> {
  117. let client = {
  118. region: 'oss-cn-shanghai',
  119. secure: true,
  120. accessKeyId: res.data.accessKeyId,
  121. accessKeySecret: res.data.accessKeySecret,
  122. securityToken: res.data.securityToken,
  123. bucket: 'ta-tech-image'
  124. }
  125. // 计算签名
  126. function computeSignature(accessKeySecret, canonicalString) {
  127. return crypto.enc.Base64.stringify(crypto.HmacSHA1(canonicalString, accessKeySecret));
  128. }
  129. const date = new Date();
  130. date.setHours(date.getHours() + 1);
  131. const policyText = {
  132. expiration: date.toISOString(), // 设置policy过期时间。
  133. conditions: [
  134. // 限制上传大小。
  135. ["content-length-range", 0, 1024 * 1024 * 1024],
  136. ],
  137. };
  138. // 获取签名
  139. const policy = Base64.encode(JSON.stringify(policyText)) // policy必须为base64的string。
  140. const signature = computeSignature(client.accessKeySecret, policy)
  141. let ossForm = that.data.ossForm
  142. ossForm = {
  143. OSSAccessKeyId: client.accessKeyId,
  144. signature,
  145. policy,
  146. SecurityToken: client.securityToken
  147. }
  148. that.setData({ossForm})
  149. }).catch(e=> {
  150. console.log(e);
  151. })
  152. },
  153. // 提交
  154. submitForm() {
  155. let that = this
  156. this.validateForm().then(res => {
  157. if(res) {
  158. let params = {}
  159. let form = this.data.form
  160. params.id = form.id
  161. params.nickname = form.nickname
  162. params.mobile = form.mobile
  163. // 需要上传oss
  164. if(form.avatar instanceof Array) {
  165. wx.showLoading({
  166. title: '上传中',
  167. mask: true
  168. })
  169. // 设置文件上传路径
  170. const randomString = Math.random().toString(36).slice(2)
  171. const timestamp = new Date().getTime()
  172. const key = `imagedir/${randomString}_${timestamp}.png`
  173. wx.uploadFile({
  174. url: 'https://ta-tech-image.oss-cn-shanghai.aliyuncs.com',
  175. filePath: form.avatar[0],
  176. name: 'file',
  177. formData: {
  178. key,
  179. OSSAccessKeyId: that.data.ossForm.OSSAccessKeyId,
  180. signature: that.data.ossForm.signature,
  181. policy: that.data.ossForm.policy,
  182. 'x-oss-security-token': that.data.ossForm.SecurityToken
  183. },
  184. success: (res)=> {
  185. if(res.statusCode === 204) {
  186. // 上传成功
  187. params.avatar = key
  188. changeMessage(params).then(res=> {
  189. wx.hideLoading()
  190. if(res.code === 0) {
  191. wx.setStorageSync('userInfo', res.data)
  192. wx.switchTab({
  193. url: '/pages/mine/mine',
  194. })
  195. }
  196. }).catch((e)=> {
  197. wx.hideLoading()
  198. console.log(e);
  199. })
  200. } else {
  201. wx.hideLoading()
  202. wx.showToast({
  203. icon: 'error',
  204. title: '图片上传失败',
  205. duration: 2000
  206. })
  207. }
  208. },
  209. fail: (e)=> {
  210. wx.hideLoading()
  211. console.log(e);
  212. }
  213. })
  214. } else {
  215. changeMessage(params).then(res=> {
  216. wx.hideLoading()
  217. if(res.code === 0) {
  218. wx.setStorageSync('userInfo', res.data)
  219. wx.switchTab({
  220. url: '/pages/mine/mine',
  221. })
  222. }
  223. }).catch((e)=> {
  224. wx.hideLoading()
  225. console.log(e);
  226. })
  227. }
  228. } else {
  229. wx.showToast({
  230. icon: 'error',
  231. title: '请完善必填信息',
  232. duration: 2000
  233. })
  234. }
  235. })
  236. }
  237. })