pyip.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // pages/pyip/pyip.js
  2. const COS = require('../../utils/cos.js');
  3. const app = require('../../utils/util.js');
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. carWin_img_hidden: true, //展示照片的view是否隐藏
  10. carWin_img: '' //存放照片路径的
  11. },
  12. openCamera() {
  13. wx.chooseMedia({
  14. count: 1,
  15. mediaType: ['image', 'video'],
  16. sourceType: ['album', 'camera'],
  17. maxDuration: 30,
  18. camera: 'back',
  19. success: (res) => {
  20. console.log(res.tempFiles[0].tempFilePath)
  21. console.log(res.tempFiles[0].size)
  22. this.setData({
  23. carWin_img: res.tempFiles[0].tempFilePath,
  24. carWin_img_hidden: false,
  25. })
  26. }
  27. })
  28. },
  29. submitImgToTxy() {
  30. if (this.data.carWin_img == "" || this.data.carWin_img == null) {
  31. wx.showModal({
  32. title: '请点击上方选择照片或者视频哦',
  33. showCancel: false
  34. });
  35. return false
  36. }
  37. wx.showLoading({
  38. mask: true,
  39. title: '加载中',
  40. })
  41. // var CosAuth = require('./cos-auth'); // COS 签名方法 https://unpkg.com/cos-js-sdk-v5/demo/common/cos-auth.min.js
  42. var Bucket = 'bucketnj1-1254259530';
  43. var Region = 'ap-nanjing';
  44. var ForcePathStyle = false; // 是否使用后缀式,涉及签名计算和域名白名单配置,后缀式说明看上文
  45. // var uploadFile = function () {
  46. // 请求用到的参数
  47. var prefix = 'https://' + Bucket + '.cos.' + Region + '.myqcloud.com/';
  48. if (ForcePathStyle) {
  49. // 后缀式请求在签名时域名使用地域域名,而不是存储桶域名,具体说明见本文上述“3.后缀式请求”
  50. prefix = 'https://cos.' + Region + '.myqcloud.com/' + Bucket + '/';
  51. }
  52. // 对更多字符编码的 url encode 格式
  53. var camSafeUrlEncode = function (str) {
  54. return encodeURIComponent(str)
  55. .replace(/!/g, '%21')
  56. .replace(/'/g, '%27')
  57. .replace(/\(/g, '%28')
  58. .replace(/\)/g, '%29')
  59. .replace(/\*/g, '%2A');
  60. };
  61. // 获取临时密钥
  62. var stsCache;
  63. var getCredentials = function (callback) {
  64. if (stsCache && Date.now() / 1000 + 30 < stsCache.expiredTime) {
  65. callback(data.credentials);
  66. return;
  67. }
  68. wx.request({
  69. method: 'POST',
  70. url: app.globalData.publicUrl + '/wx/course/getSts', // 服务端签名,参考上文说的获取临时密钥
  71. // dataType: 'json',
  72. success: function (result) {
  73. console.log(result, "000000000000")
  74. var credentials = result.data.credential.credentials;
  75. if (credentials) {
  76. stsCache = result.data.credential
  77. } else {
  78. wx.showModal({
  79. title: '临时密钥获取失败',
  80. content: JSON.stringify(data),
  81. showCancel: false
  82. });
  83. }
  84. callback(stsCache && stsCache.credentials);
  85. },
  86. error: function (err) {
  87. wx.showModal({
  88. title: '临时密钥获取失败',
  89. content: JSON.stringify(err),
  90. showCancel: false
  91. });
  92. }
  93. });
  94. };
  95. // 计算签名
  96. var getAuthorization = function (options, callback) {
  97. getCredentials(function (credentials) {
  98. callback({
  99. XCosSecurityToken: credentials.sessionToken,
  100. Authorization: COS({
  101. SecretId: credentials.tmpSecretId,
  102. SecretKey: credentials.tmpSecretKey,
  103. Method: options.Method,
  104. Pathname: options.Pathname,
  105. })
  106. });
  107. });
  108. };
  109. // 上传文件
  110. // var uploadFile = function (filePath) {
  111. var Key = this.data.carWin_img.substr(this.data.carWin_img.lastIndexOf('/') + 1); // 这里指定上传的文件名
  112. // var Key = '456abcd.png';
  113. var signPathname = '/'; // PostObject 接口 Key 是放在 Body 传输,所以请求路径和签名路径是 /
  114. if (ForcePathStyle) {
  115. // 后缀式请求在签名时用的路径,要包含存储桶名称,具体说明见本文上述“3.后缀式请求”
  116. signPathname = '/' + Bucket + '/';
  117. }
  118. getAuthorization({
  119. Method: 'POST',
  120. Pathname: signPathname
  121. }, (AuthData) => {
  122. var requestTask = wx.uploadFile({
  123. url: prefix,
  124. name: 'file',
  125. filePath: this.data.carWin_img,
  126. formData: {
  127. 'key': "paiyipai/"+Key,
  128. 'success_action_status': 200,
  129. 'Signature': AuthData.Authorization,
  130. 'x-cos-security-token': AuthData.XCosSecurityToken,
  131. 'Content-Type': '',
  132. },
  133. success: (res) => {
  134. var url = prefix + camSafeUrlEncode(Key).replace(/%2F/g, '/');
  135. console.log('prefix' + prefix);
  136. console.log('Key' + Key);
  137. console.log('4564564654654');
  138. console.log(res.statusCode);
  139. console.log(url);
  140. if (/^2\d\d$/.test('' + res.statusCode)) {
  141. wx.showModal({
  142. title: '上传成功',
  143. showCancel: false
  144. });
  145. this.setData({
  146. carWin_img: "",
  147. carWin_img_hidden:true
  148. })
  149. } else {
  150. wx.showModal({
  151. title: '上传失败',
  152. // content: JSON.stringify(res),
  153. showCancel: false
  154. });
  155. }
  156. },
  157. fail: function (res) {
  158. wx.hideLoading();
  159. wx.showModal({
  160. title: '上传失败',
  161. // content: JSON.stringify(res),
  162. showCancel: false
  163. });
  164. }
  165. });
  166. requestTask.onProgressUpdate(function (res) {
  167. console.log('进度:', res);
  168. if (res.progress == 100) {
  169. wx.hideLoading();
  170. }
  171. });
  172. });
  173. // };
  174. // }
  175. },
  176. /**
  177. * 生命周期函数--监听页面加载
  178. */
  179. onLoad: function () {
  180. },
  181. /**
  182. * 生命周期函数--监听页面初次渲染完成
  183. */
  184. onReady: function () {
  185. this.setData({
  186. carWin_img_hidden: true,
  187. carWin_img: ''
  188. });
  189. },
  190. /**
  191. * 生命周期函数--监听页面显示
  192. */
  193. onShow: function () {
  194. },
  195. /**
  196. * 生命周期函数--监听页面隐藏
  197. */
  198. onHide: function () {
  199. },
  200. /**
  201. * 生命周期函数--监听页面卸载
  202. */
  203. onUnload: function () {
  204. },
  205. /**
  206. * 页面相关事件处理函数--监听用户下拉动作
  207. */
  208. onPullDownRefresh: function () {
  209. },
  210. /**
  211. * 页面上拉触底事件的处理函数
  212. */
  213. onReachBottom: function () {
  214. },
  215. /**
  216. * 用户点击右上角分享
  217. */
  218. onShareAppMessage: function () {
  219. }
  220. })