pyip.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // pages/pyip/pyip.js
  2. const COS = require('../../utils/cos-wx-v5.js');
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. carWin_img_hidden: true, //展示照片的view是否隐藏
  9. carWin_img: '' //存放照片路径的
  10. },
  11. openCamera() {
  12. var that = this;
  13. wx.chooseImage({
  14. count: 1,
  15. success: (res) => {
  16. var filePath = res.tempFilePaths[0];
  17. that.setData({
  18. carWin_img: filePath, //把照片路径存到变量中,
  19. carWin_img_hidden: false //让展示照片的view显示
  20. });
  21. console.log(this.data.carWin_img)
  22. // 这个是使用微信接口保存文件到数据库
  23. // wx.uploadFile({
  24. // url: "",
  25. // filePath: filePath,
  26. // name: 'file',
  27. // success: function (res) {
  28. // }
  29. // })
  30. },
  31. fail: function (error) {
  32. console.error("调用本地相册文件时出错")
  33. console.warn(error)
  34. },
  35. complete: function () {
  36. }
  37. });
  38. },
  39. submitImgToTxy() {
  40. // var CosAuth = require('./cos-auth'); // COS 签名方法 https://unpkg.com/cos-js-sdk-v5/demo/common/cos-auth.min.js
  41. var Bucket = 'bucketnj1-1254259530';
  42. var Region = 'ap-nanjing';
  43. var ForcePathStyle = false; // 是否使用后缀式,涉及签名计算和域名白名单配置,后缀式说明看上文
  44. // var uploadFile = function () {
  45. // 请求用到的参数
  46. var prefix = 'https://' + Bucket + '.cos.' + Region + '.myqcloud.com/';
  47. if (ForcePathStyle) {
  48. // 后缀式请求在签名时域名使用地域域名,而不是存储桶域名,具体说明见本文上述“3.后缀式请求”
  49. prefix = 'https://cos.' + Region + '.myqcloud.com/' + Bucket + '/';
  50. }
  51. // 对更多字符编码的 url encode 格式
  52. var camSafeUrlEncode = function (str) {
  53. return encodeURIComponent(str)
  54. .replace(/!/g, '%21')
  55. .replace(/'/g, '%27')
  56. .replace(/\(/g, '%28')
  57. .replace(/\)/g, '%29')
  58. .replace(/\*/g, '%2A');
  59. };
  60. // 获取临时密钥
  61. var stsCache;
  62. var getCredentials = function (callback) {
  63. if (stsCache && Date.now() / 1000 + 30 < stsCache.expiredTime) {
  64. callback(data.credentials);
  65. return;
  66. }
  67. wx.request({
  68. method: 'GET',
  69. url: 'https://example.com/sts.php', // 服务端签名,参考上文说的获取临时密钥
  70. dataType: 'json',
  71. success: function (result) {
  72. console.log(result)
  73. var data = result.data;
  74. var credentials = data.credentials;
  75. if (credentials) {
  76. stsCache = data
  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 signPathname = '/'; // PostObject 接口 Key 是放在 Body 传输,所以请求路径和签名路径是 /
  113. if (ForcePathStyle) {
  114. // 后缀式请求在签名时用的路径,要包含存储桶名称,具体说明见本文上述“3.后缀式请求”
  115. signPathname = '/' + Bucket + '/';
  116. }
  117. getAuthorization({
  118. Method: 'POST',
  119. Pathname: signPathname
  120. }, function (AuthData) {
  121. var requestTask = wx.uploadFile({
  122. url: prefix,
  123. name: 'file',
  124. filePath: this.data.carWin_img,
  125. formData: {
  126. 'key': Key,
  127. 'success_action_status': 200,
  128. 'Signature': AuthData.Authorization,
  129. 'x-cos-security-token': AuthData.XCosSecurityToken,
  130. 'Content-Type': '',
  131. },
  132. success: function (res) {
  133. var url = prefix + camSafeUrlEncode(Key).replace(/%2F/g, '/');
  134. console.log(res.statusCode);
  135. console.log(url);
  136. if (/^2\d\d$/.test('' + res.statusCode)) {
  137. wx.showModal({
  138. title: '上传成功',
  139. content: url,
  140. showCancel: false
  141. });
  142. } else {
  143. wx.showModal({
  144. title: '上传失败',
  145. content: JSON.stringify(res),
  146. showCancel: false
  147. });
  148. }
  149. },
  150. fail: function (res) {
  151. wx.showModal({
  152. title: '上传失败',
  153. content: JSON.stringify(res),
  154. showCancel: false
  155. });
  156. }
  157. });
  158. requestTask.onProgressUpdate(function (res) {
  159. console.log('进度:', res);
  160. });
  161. });
  162. // };
  163. // }
  164. },
  165. /**
  166. * 生命周期函数--监听页面加载
  167. */
  168. onLoad: function (options) {
  169. },
  170. /**
  171. * 生命周期函数--监听页面初次渲染完成
  172. */
  173. onReady: function () {
  174. this.setData({
  175. carWin_img_hidden: true,
  176. carWin_img: ''
  177. });
  178. },
  179. /**
  180. * 生命周期函数--监听页面显示
  181. */
  182. onShow: function () {
  183. },
  184. /**
  185. * 生命周期函数--监听页面隐藏
  186. */
  187. onHide: function () {
  188. },
  189. /**
  190. * 生命周期函数--监听页面卸载
  191. */
  192. onUnload: function () {
  193. },
  194. /**
  195. * 页面相关事件处理函数--监听用户下拉动作
  196. */
  197. onPullDownRefresh: function () {
  198. },
  199. /**
  200. * 页面上拉触底事件的处理函数
  201. */
  202. onReachBottom: function () {
  203. },
  204. /**
  205. * 用户点击右上角分享
  206. */
  207. onShareAppMessage: function () {
  208. }
  209. })