upload.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. // pages/upload/upload.js
  2. const app = getApp()
  3. const $api = require('../../utils/api.js').API;
  4. const baseURL = 'https://test.hunanwanzhu.com';
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. open:true,
  11. stock_date:'',
  12. stock:[{name:'',fund:''}],
  13. stockList:[],
  14. today_stock_img:[],
  15. value:[-1],
  16. is_markt:0,
  17. today_fund:'',
  18. change:0,
  19. experience:'',
  20. cur:0,
  21. freshen:true,
  22. disable:false,
  23. id:0,
  24. images:[],
  25. },
  26. /**
  27. * 生命周期函数--监听页面加载
  28. */
  29. onShow(){
  30. if (!this.data.freshen) {
  31. return;
  32. }
  33. this.setData({
  34. disable: false
  35. })
  36. $api.initUser()
  37. wx.getStorage({
  38. key: 'user',
  39. complete: res => {
  40. this.setData({
  41. role: res.data.role,
  42. })
  43. }
  44. })
  45. $api.getCurrecord().then(res=>{
  46. if (!res.data.data.id){return}
  47. let stock = res.data.data.today_stock
  48. this.setData({
  49. stock: stock.length > 0 ? stock : [{ name: '', fund: '' }],
  50. today_stock_img: res.data.data.today_stock_img,
  51. today_fund: res.data.data.today_fund,
  52. is_markt: res.data.data.is_markt,
  53. change: 1,
  54. experience: res.data.data.experience,
  55. id:res.data.data.id
  56. })
  57. })
  58. },
  59. onLoad: function (options) {
  60. var y=new Date().getFullYear()
  61. var m=Number(new Date().getMonth() + 1)
  62. var d = new Date().getDate()
  63. m=m>9?m:'0'+m;
  64. d=d>9?d:'0'+d
  65. this.setData({
  66. stock_date:y+'-'+m+'-'+d
  67. })
  68. },
  69. /**添加持股 */
  70. add(){
  71. var stock = this.data.stock;
  72. var value=this.data.value;
  73. stock.push({ name: '', fund: '' })
  74. value.push(-1)
  75. this.setData({
  76. stock: stock,
  77. value: value
  78. })
  79. },
  80. del(e){
  81. var i = e.target.dataset.id;
  82. var stock = this.data.stock;
  83. var value = this.data.value;
  84. stock.splice(i,1)
  85. value.splice(i,1)
  86. this.setData({
  87. stock: stock,
  88. value: value
  89. })
  90. },
  91. delToday(e){
  92. this.setData({
  93. today_fund: '',
  94. })
  95. },
  96. bindPickerChange(e){
  97. var i=this.data.cur;
  98. var v = e.target.dataset.name
  99. var stock=this.data.stock;
  100. stock[i].name = v
  101. stock[i].code = e.target.dataset.id
  102. this.setData({
  103. stock: stock
  104. })
  105. setTimeout(()=>{
  106. this.setData({
  107. stockList: []
  108. },50)
  109. })
  110. return;
  111. },
  112. stockSearch(e){
  113. if (e.detail.value == ''){
  114. this.setData({
  115. stockList: []
  116. })
  117. return
  118. }
  119. var i = e.target.dataset.id;
  120. var v = e.detail.value
  121. var stock = this.data.stock;
  122. stock[i].name = v;
  123. this.setData({
  124. stock: stock
  125. })
  126. $api.searchStock({ name: e.detail.value}).then(res=>{
  127. this.setData({
  128. stockList:res.data.data,
  129. cur:e.target.dataset.id
  130. })
  131. })
  132. },
  133. blur(){
  134. // console.log(233)
  135. this.setData({
  136. stockList: []
  137. })
  138. },
  139. nameChange(e){
  140. console.log(e)
  141. var i = e.target.dataset.id;
  142. var v = e.detail.value
  143. var stock = this.data.stock;
  144. stock[i].name = v;
  145. this.setData({
  146. stock: stock
  147. })
  148. },
  149. moneyChange(e){
  150. var i = e.target.dataset.id;
  151. var v = e.detail.value
  152. var stock = this.data.stock;
  153. if (v.split('.').length > 1 && v.split('.')[1].length > 4) {
  154. v = Math.floor(Number(v) * 10000) / 10000
  155. }
  156. stock[i].money = v;
  157. this.setData({
  158. stock: stock
  159. })
  160. },
  161. fundChange(e){
  162. var i = e.target.dataset.id;
  163. var v = e.detail.value
  164. var stock = this.data.stock;
  165. if (v.split('.').length > 1 && v.split('.')[1].length > 4) {
  166. v = Math.floor(Number(v) * 10000) / 10000
  167. }
  168. stock[i].fund = v;
  169. this.setData({
  170. stock: stock
  171. })
  172. },
  173. fundChange1(e) {
  174. var v = e.detail.value
  175. if (v.split('.').length > 1 && v.split('.')[1].length > 4){
  176. v = Math.floor(Number(v)*10000)/10000
  177. }
  178. this.setData({
  179. today_fund: v
  180. })
  181. },
  182. expChange(e){
  183. var v = e.detail.value
  184. this.setData({
  185. experience: v
  186. })
  187. },
  188. switch1Change(e){
  189. this.setData({
  190. is_markt: Number(e.detail.value)
  191. })
  192. },
  193. // oss验签上传图片
  194. upload() {
  195. const that = this
  196. var today_stock_img = that.data.today_stock_img;
  197. wx.chooseMedia({
  198. mediaType: ['image'], // 只能上传图片 还有个属性是可以上传视频
  199. success(res) {
  200. wx.showLoading({
  201. title: '上传中',
  202. mask: true
  203. })
  204. let imgs = []
  205. let valueArr = []
  206. // 需要oss 验签 此处是调取验签的接口
  207. $api.getOssSign(res).then((r) => {
  208. wx.hideLoading()
  209. const arr = res.tempFiles
  210. // arr 是为了获取上传图片反的数组
  211. arr.forEach((e) => {
  212. return imgs.push(e.tempFilePath)
  213. })
  214. for (var i = 0; i < imgs.length; i++) {
  215. let only = imgs[i].split('.')
  216. valueArr.push(
  217. only[only.length - 1]
  218. )
  219. let filename = parseInt(Math.random() * 1000000 + new Date().getTime()) + '.' + valueArr[valueArr.length - 1]
  220. let httpData = {
  221. Key: r.data.dir + filename,
  222. OSSAccessKeyId: r.data.AccessKeyID,
  223. Policy: r.data.policy_encode,
  224. Signature: r.data.signature,
  225. }
  226. wx.uploadFile({
  227. url: baseURL+'/api/wx/uploadfile',
  228. filePath: imgs[i],
  229. formData: httpData,
  230. fileType: 'image',
  231. name: 'file',
  232. timeout: 15000,
  233. header: {
  234. 'Content-Type': 'application/x-www-form-urlencoded',
  235. },
  236. success: (res) => {
  237. if (res.statusCode == 200) {
  238. var data=JSON.parse(res.data)
  239. today_stock_img.push(data.data.url)
  240. that.setData({
  241. today_stock_img: today_stock_img
  242. })
  243. } else {
  244. // 返回错误信息
  245. console.log('error')
  246. }
  247. },
  248. fail: function (res) {
  249. console.log(res);
  250. }
  251. })
  252. }
  253. })
  254. }
  255. })
  256. },
  257. loadOcr(path,url){
  258. var _this = this
  259. wx.getImageInfo({
  260. src:path,
  261. success:function(ires){
  262. _this.loadImageOcr(url,ires.width < ires.height ? 'phone':'pc')
  263. }
  264. })
  265. },
  266. loadImageOcr(img_url,source){
  267. var _this = this
  268. var param = {
  269. img_url:img_url.split('?')[0],
  270. source:source,
  271. }
  272. console.log('img_url',param['img_url'])
  273. console.log('source',param['source'])
  274. $api.imageOcr(param).then(res => {
  275. console.log('ocr',res)
  276. if(res.data.code == 0){
  277. _this.setData({
  278. today_fund:res.data.data.today,
  279. stock:res.data.data.list
  280. })
  281. }
  282. })
  283. },
  284. startOcr(e){
  285. var index = e.target.dataset.index;
  286. var img_url = this.data.today_stock_img[index]
  287. console.log('startocr',img_url)
  288. this.loadImageOcr(img_url,this.data.images[index].width < this.data.images[index].height ? 'phone':'pc')
  289. },
  290. imageLoad: function(e) {
  291. var width=e.detail.width; //获取图片真实宽度
  292. var height=e.detail.height;
  293. var image=this.data.images;
  294. //将图片的datadata-index作为image对象的key,然后存储图片的宽高值
  295. image[e.target.dataset.index]={
  296. width:width,
  297. height:height,
  298. }
  299. this.setData({
  300. images:image
  301. })
  302. },
  303. del1(e) {
  304. var i = e.target.dataset.id;
  305. var today_stock_img = this.data.today_stock_img;
  306. today_stock_img.splice(i, 1)
  307. this.setData({
  308. today_stock_img: today_stock_img
  309. })
  310. },
  311. prview(e){
  312. var src = e.target.dataset.src;
  313. wx.previewImage({
  314. current: src, // 当前显示图片的http链接
  315. urls: [src] // 需要预览的图片http链接列表
  316. })
  317. },
  318. post(){
  319. var stocks = []
  320. var slist = this.data.stock
  321. for(let i=0;i<slist.length;i++){
  322. if(slist[i].name.length <= 0){
  323. continue;
  324. }
  325. if(slist[i].fund > 5000){
  326. wx.showToast({
  327. icon: 'none',
  328. title: '持仓金额单位为万'
  329. })
  330. return;
  331. }
  332. stocks.push(slist[i])
  333. }
  334. var parm = {
  335. stock_date: this.data.stock_date,
  336. today_stock: stocks,
  337. is_markt: this.data.is_markt,
  338. today_stock_img: this.data.today_stock_img,
  339. today_fund: Number(this.data.today_fund),
  340. id:this.data.id,
  341. experience: this.data.experience
  342. }
  343. if (!parm.today_fund) {
  344. wx.showToast({
  345. icon: 'none',
  346. title: '请输入今日净资产'
  347. })
  348. return
  349. }
  350. if (parm.today_fund >= 10000) {
  351. wx.showToast({
  352. icon: 'none',
  353. title: '今日净资产单位为万'
  354. })
  355. return
  356. }
  357. // if (!parm.is_markt) {
  358. // // for (let i = 0; i < parm.today_stock.length;i++){
  359. // // if (!parm.today_stock[i].name || !parm.today_stock[i].fund) {
  360. // // wx.showToast({
  361. // // icon: 'none',
  362. // // title: '请选择股票并输入资金额'
  363. // // })
  364. // // return;
  365. // // break;
  366. // // }
  367. // // }
  368. // } else {
  369. // parm.today_stock = []
  370. // }
  371. console.log(parm)
  372. if (parm.today_stock_img.length <= 0) {
  373. wx.showToast({
  374. icon: 'none',
  375. title: '请上传截图'
  376. })
  377. return
  378. }
  379. wx.showLoading({
  380. title: '正在提交',
  381. })
  382. this.setData({
  383. disable: true
  384. })
  385. $api.updateRecord(parm).then(res => {
  386. wx.hideLoading()
  387. wx.showToast({
  388. title: '提交成功',
  389. })
  390. this.setData({
  391. freshen: true,
  392. disable: false
  393. })
  394. setTimeout(() => {
  395. this.setData({
  396. stock: [{ label: '', name: '', code: '', fund: '' }],
  397. stockList: [],
  398. today_stock_img: [],
  399. value: [-1],
  400. is_markt: 0,
  401. today_fund: ''
  402. })
  403. wx.switchTab({
  404. url: '../user/user',
  405. })
  406. }, 200)
  407. })
  408. .catch(
  409. this.setData({
  410. disable:false
  411. })
  412. )
  413. },
  414. /**
  415. * 生命周期函数--监听页面初次渲染完成
  416. */
  417. onReady: function () {
  418. },
  419. /**
  420. * 生命周期函数--监听页面隐藏
  421. */
  422. onHide: function () {
  423. },
  424. /**
  425. * 生命周期函数--监听页面卸载
  426. */
  427. onUnload: function () {
  428. },
  429. /**
  430. * 页面相关事件处理函数--监听用户下拉动作
  431. */
  432. onPullDownRefresh: function () {
  433. },
  434. /**
  435. * 页面上拉触底事件的处理函数
  436. */
  437. onReachBottom: function () {
  438. },
  439. /**
  440. * 用户点击右上角分享
  441. */
  442. onShareAppMessage: function () {
  443. }
  444. })