记账备注信息添加
添加点击事件,判断备注信息是否为空,不为空则显示内容,为空则显示默认的添加备注信息
<a class="color-blue" bindtap="edit_bt" href="#"><block wx:if="{{content}}">{{content}}</block><block wx:else>添加备注</block></a>
在data数据中添加content字段默认为空,事件调用模态框获取并设置备注信息到data中
edit_bt(e){wx.showModal({title: '请输入备注',content: this.data.content,editable: true,success:(e)=>{// 判断是否点击了确认if(e.confirm){this.setData({content: e.content})}}})}


收入和支出Tab栏切换时,再次返回来重新默认选择收入或支出下面的第一个分类图标
// 收入和支出tab栏切换事件changeTab: function( e ) {// 当点击支出的时候,支出是1 收入是2// 为支出1的时候,我们把支出分类全部拿出来,this.data.zhichuif(e.currentTarget.dataset.current==1){var zhichu = this.data.zhichu;// 目的是当点击回到支出的时候,让他重新选择第一个// 拿到数据后,进行循环吧图标改变,第一个也就是第0项,修改为选中图标,其他全部修改为未选中for(var i=0;i<zhichu.length;i++){if(i==0){// 将当前选中的索引也赋值为0zhichu_select = 0;// 将当前的分类id赋值记录cat_id = zhichu[i].id;// 将第一个设置为选中zhichu[i].select_img = zhichu[i].img_s;}else{// 如果不是第一个全部设置为未选中zhichu[i].select_img = zhichu[i].img;}}this.setData({zhichu:zhichu})}else{var shouru = this.data.shouru;for(var i=0;i<shouru.length;i++){if(i==0){shouru_select = 0;cat_id = shouru[i].id;shouru[i].select_img = shouru[i].img_s;}else{shouru[i].select_img = shouru[i].img;}}this.setData({shouru:shouru})}// 存储当前选中的Tab栏是哪一个 状态:1支出 2收入this.setData({currentData:e.currentTarget.dataset.current})},
前面做了支出金额的添加和支出分类图片的选中,这里来完善收入分类的图片选中及添加信息
select_cat(e){console.log(e.currentTarget.dataset.key);// 选中的key 也就是分类的IDvar key = e.currentTarget.dataset.key;// currentData=1 支出页面图标选择if(this.data.currentData==1){// 判断当前选中如果不是自己才执行if(key != zhichu_select){// 修改当前选中的key的img为选中图片this.data.zhichu[key].select_img = this.data.zhichu[key].img_s;// key === zhichu_select 将上一次选中的修改为未选中图片this.data.zhichu[zhichu_select].select_img = this.data.zhichu[zhichu_select].img;// 将这次的key赋值zhichu_select = key;// 同步数据this.setData({zhichu : this.data.zhichu})// 将当前选中的id赋值给cat_id = e.currentTarget.dataset.id;}}else{// 收入页面图标选择// 判断当前选中如果不是自己才执行if(key != shouru_select){// 修改当前选中的key的img为选中图片this.data.shouru[key].select_img = this.data.shouru[key].img_s;// key === shouru_select 将上一次选中的修改为未选中图片this.data.shouru[shouru_select].select_img = this.data.shouru[shouru_select].img;// 将这次的key赋值shouru_select = key;// 同步数据this.setData({shouru : this.data.shouru})// 将当前选中的id赋值给cat_id = e.currentTarget.dataset.id;}}},submit(){// 请求添加记账明细wx.request({url : 'https://jz.easys.ltd/index.php/api/Jizhang/add_jizhang',data : {input_v : this.data.input_v, //金额type : this.data.currentData, // 类型uid : 1, // 用户IDcid : cat_id, // 分类IDdate : this.data.date, // 日期remarks : this.data.content // 备注},method : 'POST',success:(e)=>{// 如果返回的code不为1,表示添加失败if(e.data.code != 0){wx.showToast({title: e.data.msg,icon: 'error',duration: 2000})return false;}else{// 记账成功提示wx.showToast({title: e.data.msg,icon: 'success',duration: 2000});// 清空还原所有数据this.setData({currentData: 1,input_v : '',zhichu : default_zhichu,shouru : default_shouru,date : default_shijian,week : default_week,content: '',})}}})}
格式化当前选中的时间,传给后台接口,将其2021-06-06转换为年月日星期几形式的数据
// 执行日期的选中方法edit_date(e){var date = e.detail.valueconsole.log(date);// 将当前选择的时间请求发送给后台接口wx.request({url: 'https://jz.easys.ltd/index.php/api/Jizhang/format_date',data: {date},method : 'POST',success:(e)=>{this.setData({date: e.data.data.date,week: e.data.data.week})}});},
后台处理格式化时间接口
<?phpnamespace app\api\controller;use app\api\controller\Base;use think\facade\Db;use think\facade\Request;class Jizhang extends Base{// 获取分类列表接口public function cat_list(){// 查询支出分类列表,并处理图片链接$zhichu = Db::table('oyk_cat')->where('status',1)->where('type',1)->select()->each(function($res){$res['img'] = 'https://'.$_SERVER['SERVER_NAME'] . $res['img'];$res['img_s'] = 'https://'.$_SERVER['SERVER_NAME'] . $res['img_s'];return $res;});// 查询收入分类列表,并处理图片链接$shouru = Db::table('oyk_cat')->where('status',1)->where('type',2)->select()->each(function($res){$res['img'] = 'https://'.$_SERVER['SERVER_NAME']. $res['img'];$res['img_s'] = 'https://'.$_SERVER['SERVER_NAME'] . $res['img_s'];return $res;});// 返回当前时间格式化年月日数据$date = date('Y年m月d日',time());$arr = ['zhichu' => $zhichu,'shouru' => $shouru,'date' => $date];$this->returnCode(0,$arr);}// 添加记账记录数据public function add_jizhang(){// 判断是否输入金额$input_v = Request::post('input_v');if(empty($input_v)){$this->returnCode(1,'请输入数字');}// 接受添加记录数据$uid = Request::post('uid');$cid = Request::post('cid',1);$date = Request::post('date');$type = Request::post('type',1);$remarks = Request::post('remarks','');// 处理前台返回的年月日时间$date =str_replace('年','-',$date);$date =str_replace('月','-',$date);$date =str_replace('日','-',$date);// 执行添加操作$insert = Db::table('oyk_jz')->insert(['uid' => $uid,'cid' => $cid,'date' => $date,'type' => $type,'money' => $input_v,'add_time' => time(),'remarks' => $remarks,]);// 返回错误信息if(empty($insert)){$this->returnCode(1,'记录失败');}$this->returnCode(0,'成功');}// 将时间格式化为年月日星期几形式public function format_date(){$date = Request::post('date');if(empty($date)){$this->returnCode(1,'请选择年月日');}// 获取当前时间为星期几$array = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'];// 将传递过来的时间转换为时间戳$time = strtotime($date);// 获取星期几$week = $array[date('w',$time)];// 年月日// $time = explode('-'$date);// $date = $time[0].'年'.$time[1]."月".$time[0].'日';$date = date('Y年m月d日',$time);$this->returnCode(0,['week'=>$week,'date'=>$date]);}}


完整记账页面代码
index.wxml
<!--index.wxml--><view class="wrapper-bg"><view class="income-wrap"><view class="income-header"><!-- 支出和收入切换 --><view class="layout-row layout-align-start-start income-tab"><view class="{{currentData == 1 ? 'outlay' : ''}}" data-current="1" bindtap="changeTab">支出<text data-current="0" class="tab-active"></text></view><view class="{{currentData == 2 ? 'income-nav' : ''}}" data-current="2" bindtap="changeTab">收入<text data-current="1" class="tab-active"></text></view></view><!-- 金额输入框 --><view class="int layout-row layout-align-start-start"><text style="{{currentData == 2 ? 'color:#cda337' : ''}}">¥</text><input value="{{input_v}}" disabled></input></view></view><scroll-view scroll-y='ture' class="income-center text-center"><!-- 支出分类列表 --><view class="layout-row layout-align-start-start layout-wrap" hidden="{{currentData != 1}}"><view class="in-icon" wx:for="{{zhichu}}" wx:key="index" bindtap="select_cat" data-id="{{item.id}}" data-key="{{index}}"><image src="{{item.select_img}}"></image><text>{{item.name}}</text></view></view><!-- 收入分类列表 --><view class="layout-row layout-align-start-start layout-wrap" hidden="{{currentData != 2}}"><view class="in-icon" wx:for="{{shouru}}" wx:key="index" bindtap="select_cat" data-id="{{item.id}}" data-key="{{index}}"><image src="{{item.select_img}}"></image><text>{{item.name}}</text></view></view></scroll-view><!-- 备注和日期 --><view class="income-footer layout-row layout-align-space-between-center"><a class="color-blue" bindtap="edit_bt" href="#"><block wx:if="{{content}}">{{content}}</block><block wx:else>添加备注</block></a><picker mode="date" class="income-time" bindchange="edit_date">{{date}} <text>{{week}}</text></picker></view></view><!-- 数字键 --><view class="calculator layout-row layout-align-start-start"><view class="calculator-left layout-row layout-align-start-center layout-wrap text-center"><view class="calculator-key" bindtap="input_num" data-num="1">1</view><view class="calculator-key" bindtap="input_num" data-num="2">2</view><view class="calculator-key" bindtap="input_num" data-num="3">3</view><view class="calculator-key" bindtap="input_num" data-num="4">4</view><view class="calculator-key" bindtap="input_num" data-num="5">5</view><view class="calculator-key" bindtap="input_num" data-num="6">6</view><view class="calculator-key" bindtap="input_num" data-num="7">7</view><view class="calculator-key" bindtap="input_num" data-num="8">8</view><view class="calculator-key" bindtap="input_num" data-num="9">9</view><view class="calculator-key calculator-in" bindtap="input_num" data-num="0">0</view><view class="calculator-key" bindtap="input_num" data-num=".">.</view></view><view class="calculator-right"><view class="calculator-delete" style="text-align:center;" bindtap="input_del"><image style="width:40rpx;height:40rpx;margin-top:20rpx;" src="/img/del.png"></image></view><view class="calculator-enter" bindtap="submit">确定</view></view></view></view>
index.js
var zhichu_select = 0;var cat_id = 0; // 要传给接口的idvar shouru_select = 0;// 初始化默认数据var default_zhichu ;var default_shouru ;var default_shijian;var default_week;Page({data: {currentData: 1, // 状态:1支出 2收入input_v : '',zhichu : [],shouru : [],date : '',week: '',content: ''},onLoad: function (options) {wx.request({url : 'http://jz.easys.ltd/index.php/api/Jizhang/cat_list',data :{},method:'POST',success:(e)=>{var zhichu = e.data.data.zhichu;// 循环支出列表for(var i=0;i<zhichu.length;i++){if(i == 0){// 如果是第一项,将id赋值给当前的idcat_id = zhichu[i].id; // 第一次获取接口时,默认也第一个分类的id,就是要传的id,是选中的分类id// 将选中图标复制给变量select_img,我们在渲染列表的时候就用select_img默认第一项是选中的图片zhichu[i].select_img = zhichu[i].img_s;}else{// 否则则赋值未选中的图片zhichu[i].select_img = zhichu[i].img;}}// 收入列表同上var shouru = e.data.data.shouru;for(var i=0;i<shouru.length;i++){if(i==0){shouru[i].select_img = shouru[i].img_s;}else{shouru[i].select_img = shouru[i].img;}}this.setData({zhichu : zhichu,shouru : shouru,date : e.data.data.date,week: e.data.data.week});// 记录初始化的默认数据default_zhichu = zhichu;default_shouru = shouru;default_shijian = e.data.data.date;default_week = e.data.data.week;}})},changeTab: function( e ) {// 当点击支出的时候,支出是1 收入是2// 为支出1的时候,我们把支出分类全部拿出来,this.data.zhichuif(e.currentTarget.dataset.current==1){var zhichu = this.data.zhichu;// 目的是当点击回到支出的时候,让他重新选择第一个// 拿到数据后,进行循环吧图标改变,第一个也就是第0项,修改为选中图标,其他全部修改为未选中for(var i=0;i<zhichu.length;i++){if(i==0){zhichu_select = 0;cat_id = zhichu[i].id;zhichu[i].select_img = zhichu[i].img_s;}else{zhichu[i].select_img = zhichu[i].img;}}this.setData({zhichu:zhichu})}else{var shouru = this.data.shouru;for(var i=0;i<shouru.length;i++){if(i==0){shouru_select = 0;cat_id = shouru[i].id;shouru[i].select_img = shouru[i].img_s;}else{shouru[i].select_img = shouru[i].img;}}this.setData({shouru:shouru})}// 存储当前选中的Tab栏是哪一个this.setData({currentData:e.currentTarget.dataset.current})},bindChange: function( e ) {this.setData( { currentTab: e.detail.current });},select_cat(e){console.log(e.currentTarget.dataset.key);// 选中的key 也就是分类的IDvar key = e.currentTarget.dataset.key;// current=1 支出页面图标选择if(this.data.currentData==1){// 判断当前选中如果不是自己才执行if(key != zhichu_select){// 修改当前选中的key的img为选中图片this.data.zhichu[key].select_img = this.data.zhichu[key].img_s;// key === zhichu_select 将上一次选中的修改为未选中图片this.data.zhichu[zhichu_select].select_img = this.data.zhichu[zhichu_select].img;// 将这次的key赋值zhichu_select = key;// 同步数据this.setData({zhichu : this.data.zhichu})// 将当前选中的id赋值给cat_id = e.currentTarget.dataset.id;}}else{// 收入页面图标选择// 判断当前选中如果不是自己才执行if(key != shouru_select){// 修改当前选中的key的img为选中图片this.data.shouru[key].select_img = this.data.shouru[key].img_s;// key === shouru_select 将上一次选中的修改为未选中图片this.data.shouru[shouru_select].select_img = this.data.shouru[shouru_select].img;// 将这次的key赋值shouru_select = key;// 同步数据this.setData({shouru : this.data.shouru})// 将当前选中的id赋值给cat_id = e.currentTarget.dataset.id;}}},// 执行日期的选中方法edit_date(e){var date = e.detail.valueconsole.log(date);// 将当前选择的时间请求发送给后台接口wx.request({url: 'https://jz.easys.ltd/index.php/api/Jizhang/format_date',data: {date},method : 'POST',success:(e)=>{this.setData({date: e.data.data.date,week: e.data.data.week})}});},input_num(e){// 获取点击的数字按钮的自定义属性datasetvar input_v = e.target.dataset.num;// 判断当前点击的是否为小数点if(input_v == '.'){// 如果小数点已经存在则累加一个空值if(this.data.input_v.indexOf(".") != -1 ){input_v = '';}// 如果还没有输入内容则不能输入小数点if(!this.data.input_v){input_v = '';}// 判断点击的是否是0}else if(input_v == 0){// 如果值已经是0则不在添加0,否则则添加0if(this.data.input_v == '0'){input_v = '';}else{input_v = 0;}}if(this.data.input_v>=100000){wx.showToast({title: '请输入小于10万的值',icon: 'error'});input_v = '';}this.setData({// 将当前输入的值和原始值合并input_v : this.data.input_v.concat(input_v)})},input_del(e){var input_v = this.data.input_v;// 删除最后一个字符input_v = input_v.substr(0, input_v.length - 1);this.setData({input_v : input_v})},submit(){// 请求添加记账明细wx.request({url : 'https://jz.easys.ltd/index.php/api/Jizhang/add_jizhang',data : {input_v : this.data.input_v, //金额type : this.data.currentData, // 类型uid : 1, // 用户IDcid : cat_id, // 分类IDdate : this.data.date, // 日期remarks : this.data.content // 备注},method : 'POST',success:(e)=>{// 如果返回的code不为1,表示添加失败if(e.data.code != 0){wx.showToast({title: e.data.msg,icon: 'error',duration: 2000})return false;}else{// 记账成功提示wx.showToast({title: e.data.msg,icon: 'success',duration: 2000});// 清空还原所有数据this.setData({currentData: 1,input_v : '',zhichu : default_zhichu,shouru : default_shouru,date : default_shijian,week : default_week,content: '',})}}})},// 添加备注edit_bt(e){wx.showModal({title: '请输入备注',content: this.data.content,editable: true,success:(e)=>{// 判断是否点击了确认if(e.confirm){this.setData({content: e.content})}}})}})
登录流程时序

说明:
之后开发者服务器可以根据用户标识来生成自定义登录态,用于后续业务逻辑中前后端交互时识别用户身份。
注意:
session_key 是对用户数据进行 加密签名 的密钥。为了应用自身的数据安全,开发者服务器不应该把会话密钥下发到小程序,也不应该对外提供这个密钥。调用接口获取登录凭证(code)。通过凭证进而换取用户登录态信息,包括用户在当前小程序的唯一标识(openid)、微信开放平台帐号下的唯一标识(unionid,若当前小程序已绑定到微信开放平台帐号)及本次登录的会话密钥(session_key)等。用户数据的加解密通讯需要依赖会话密钥完成。
https://developers.weixin.qq.com/miniprogram/dev/api/open-api/login/wx.login.html
登录凭证校验。通过 wx.login 接口获得临时登录凭证 code 后传到开发者服务器调用此接口完成登录流程。
https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
我的页面获取微信头像和微信登陆功能开发
主要思路:当用户第一次进入小程序未点击登陆的时候,我们将默认创建一个用户,返回uid,当用户点击了登陆获取到唯一识别码openid时,在修改对应uid的用户信息,将其openid添加进去。小程序端通过缓存来保持用户登陆状态。
<!--pages/my/index.wxml--><view style="padding:130rpx 13rpx 13rpx; font-size: 30rpx;background:url('../../img/background.png')"><view style="text-align:center;background: rgb(251 250 250 / 0.8);box-shadow: 3px 3px 6px #eee;padding: 30rpx;border-radius: 4%;" ><image src="{{userInfo['avatarUrl']}}" style="width:160rpx;height:160rpx;border-radius:50%;margin-bottom:22rpx;"></image><view>{{userInfo['nickName']}}</view><view><text>地区:{{userInfo['province']}} {{userInfo['city']}}</text><text> 性别:<block wx:if="{{userInfo['gender']==1}}"> 男</block><block wx:elif="{{userInfo['gender']==2}}"> 女</block><block wx:else> 未知</block></text></view><block wx:if="{{!hasUserInfo}}"><button style="margin-top:20rpx;background:#eee" bindtap="getUserProfile">使用微信头像</button></block><block wx:if="{{!hasLogin}}"><button style="margin-top:20rpx;" plain="true" open-type='getUserInfo' bindgetuserinfo='bindGetUserInfo' lang="zh_CN">使用微信登陆</button></block></view><view style="text-align:center;display:flex;margin-top:40rpx;background: rgb(251 250 250 / 0.8);padding: 30rpx 15rpx;box-shadow: 3px 3px 6px #eee;"><view style="width:33%;"><view style="color:#CFA348">+{{shouru}}</view><view style="font-size:26rpx;">年收入</view></view><view style="width:34%"><view>-{{zhichu}}</view><view style="font-size:26rpx;">年支出</view></view><view style="width:33%" bindtap="setMoney"><view wx:if="{{yusuan == 0}}">未设置</view><view wx:elif="{{yusuan < 0}}" style="color:red;">{{yusuan}}</view><view wx:elif="{{yusuan > 0}}" style="color:green;">{{yusuan}}</view><view style="font-size:26rpx;">月预算余额</view></view></view><view style="text-align:center;display:flex;margin-top:40rpx;background: rgb(251 250 250 / 0.8);padding: 30rpx;box-shadow: 3px 3px 6px #eee;"><view style="width:20%;" ><image src="{{userInfo['avatarUrl']}}" style="width:100rpx;height:100rpx;border-radius:50%;"></image><view style="font-size:20rpx;">{{userInfo['nickName']}}</view></view><view style="width:20%;"><button style="width:100%;padding:0;background: transparent;" open-type="share" ><image src="../../img/index.png" style="width:90rpx;height:90rpx;border-radius:50%;"></image><view>添加</view></button></view></view><view style="margin-top:40rpx;background: rgb(251 250 250 / 0.8);padding: 26rpx;box-shadow: 3px 3px 6px #eee;"><button style="width:100%;padding:0;text-align: left;background: transparent;" open-type="feedback"><view style="border-bottom: 3px solid #eee;padding: 10px 0;" ><image src="../../img/feedback.png" style="width:30px;height:30px;padding-right:20px"></image><text style="vertical-align: super;">反馈问题</text></view></button><button style="width:100%;padding:0;text-align: left;background: transparent;" open-type="share"><view style="width:100%;border-bottom: 3px solid #eee;padding: 10px 0;" ><image src="../../img/share.png" style="width:30px;height:30px;padding-right:20px"></image><text style="vertical-align: super;">推荐好友</text></view></button><view style="margin-top:26rpx;"><text style="font-size:30rpx">设置手机亮度</text><slider value="{{Brightness}}" bindchange="sliderchange" min="0" max="100" step='1' left-icon="cancel" show-value right-icon="success_no_circle"/></view></view><view style="margin-top:26rpx;"><text>当前手机亮度:<block wx:if="{{Brightness==100}}">闪瞎眼</block><block wx:elif="{{Brightness>=80}}">刺眼</block><block wx:elif="{{Brightness>=60}}">超量</block><block wx:elif="{{Brightness>=40}}">适合</block><block wx:elif="{{Brightness>=20}}">较暗</block><block wx:else>看不见</block></text><progress percent="{{Brightness}}" show-info border-radius='50' stroke-width="10"/></view><view style="margin-top:26rpx;"><text>当前手机电量:<block wx:if="{{isCharging}}">正在充电</block></text><progress percent="{{level}}" show-info border-radius='50' stroke-width="10"/></view></view>
// pages/my/index.jsPage({/*** 页面的初始数据*/data: {userInfo :'',hasUserInfo: false,hasLogin: false,shouru:100000,zhichu:30000,yusuan:0,Brightness: 0,level:'',isCharging:false},getUserProfile(){if(!this.data.hasUserInfo){wx.getUserProfile({desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写lang:'zh_CN',success: (res) => {console.log(res);this.setData({userInfo: res.userInfo,hasUserInfo: true});wx.setStorage({key:"userInfo",data: res.userInfo})}});}},bindGetUserInfo: function(res) {if (res.detail.userInfo) {var nickName = res.detail.userInfo.nickName;var avatarUrl = res.detail.userInfo.avatarUrl;var gender = res.detail.userInfo.gender;var province = res.detail.userInfo.province;var city = res.detail.userInfo.city;var userInfo = res.detail.userInfo;wx.login({success:(res)=>{if(!res.code){wx.showToast({title: '微信授权失败,请刷新重试',icon: 'none',duration: 1200})return;}wx.request({url: 'https://jz.easys.ltd/index.php/api/Jizhang/wx_login',data: {code: res.code,nickName:nickName,avatarUrl:avatarUrl,gender:gender,province:province,city:city},method : 'POST',success:(e)=>{if(e.data.code!=0){wx.showToast({title: e.data.msg,icon: 'error',duration: 2000});return false;}console.log(userInfo);wx.setStorage({key: 'userInfo',data: userInfo})wx.setStorage({key: 'user',data: e.data.data});this.setData({hasLogin: true,hasUserInfo:true,userInfo: userInfo})}});},fail:()=>{wx.showToast({title: '网络错误,请刷新重试',icon: 'none',duration: 1200});}})}},sliderchange(event){// 获取当前滑块的值console.log(event.detail.value);this.setData({Brightness: parseInt(event.detail.value)});// 设置手机屏幕亮度0~1wx.setScreenBrightness({value: this.data.Brightness/100,success(e){console.log(e);}});// 获取屏幕亮度wx.getScreenBrightness({success:(e)=>{console.log("屏幕当前亮度:",e.value);}});},/*** 生命周期函数--监听页面加载*/onLoad: function (options) {wx.getStorage({key: 'user',success:(res)=>{console.log(res.data)this.setData({hasLogin: true});wx.request({url: 'https://jz.easys.ltd/index.php/api/Jizhang/user_info',data: {uid: res.data.uid},method: 'POST',success:(e)=>{this.setData({shouru: e.data.data.shouru,zhichu: e.data.data.zhichu,yusuan: e.data.data.yue,})}})}})wx.getStorage({key: 'userInfo',success:(res)=>{console.log(res.data)this.setData({userInfo: res.data,hasUserInfo: true});},fail:(e)=>{wx.getUserInfo({lang: 'zh_CN',success: (res)=>{this.setData({userInfo: res.userInfo})}});}})// 获取手机屏幕亮度wx.getScreenBrightness({success:(e)=>{this.setData({Brightness: Math.ceil(e.value*100)})}});// 同步调用获取电量const BatteryInfo = wx.getBatteryInfoSync();// 设备电量,范围 1 - 100console.log(BatteryInfo.level);// 是否正在充电中console.log(BatteryInfo.isCharging);this.setData({level: BatteryInfo.level,isCharging: BatteryInfo.isCharging})},/*** 生命周期函数--监听页面初次渲染完成*/onReady: function () {},/*** 生命周期函数--监听页面显示*/onShow: function () {},/*** 生命周期函数--监听页面隐藏*/onHide: function () {},/*** 生命周期函数--监听页面卸载*/onUnload: function () {},/*** 页面相关事件处理函数--监听用户下拉动作*/onPullDownRefresh: function () {},/*** 页面上拉触底事件的处理函数*/onReachBottom: function () {},/*** 用户点击右上角分享*/onShareAppMessage: function () {wx.showShareMenu({withShareTicket: true,menus: ['shareAppMessage', 'shareTimeline']});}})
后台数据接口
<?phpnamespace app\api\controller;use app\api\controller\Base;use think\facade\Db;use think\facade\Request;class Jizhang extends Base{// 获取分类列表接口public function cat_list(){// 查询支出分类列表,并处理图片链接$zhichu = Db::table('oyk_cat')->where('status',1)->where('type',1)->select()->each(function($res){$res['img'] = 'https://'.$_SERVER['SERVER_NAME'] . $res['img'];$res['img_s'] = 'https://'.$_SERVER['SERVER_NAME'] . $res['img_s'];return $res;});// 查询收入分类列表,并处理图片链接$shouru = Db::table('oyk_cat')->where('status',1)->where('type',2)->select()->each(function($res){$res['img'] = 'https://'.$_SERVER['SERVER_NAME']. $res['img'];$res['img_s'] = 'https://'.$_SERVER['SERVER_NAME'] . $res['img_s'];return $res;});// 返回当前时间格式化年月日数据$date = date('Y年m月d日',time());$array = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'];$week = $array[date('w',time())];$arr = ['zhichu' => $zhichu,'shouru' => $shouru,'date' => $date,'week'=>$week,];$this->returnCode(0,$arr);}// 添加记账记录数据public function add_jizhang(){// 判断是否输入金额$input_v = Request::post('input_v');if(empty($input_v)){$this->returnCode(1,'请输入数字');}// 接受添加记录数据$uid = Request::post('uid');$cid = Request::post('cid',1);$date = Request::post('date');$type = Request::post('type',1);$remarks = Request::post('remarks','');// 处理前台返回的年月日时间$date =str_replace('年','-',$date);$date =str_replace('月','-',$date);$date =str_replace('日','-',$date);// 执行添加操作$insert = Db::table('oyk_jz')->insert(['uid' => $uid,'cid' => $cid,'date' => $date,'type' => $type,'money' => $input_v,'add_time' => time(),'remarks' => $remarks,]);// 返回错误信息if(empty($insert)){$this->returnCode(1,'记录失败');}$this->returnCode(0,'成功');}// 将时间格式化为年月日星期几形式public function format_date(){$date = Request::post('date');if(empty($date)){$this->returnCode(1,'请选择年月日');}// 获取当前时间为星期几$array = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'];// 将传递过来的时间转换为时间戳$time = strtotime($date);// 获取星期几$week = $array[date('w',$time)];// 年月日// $time = explode('-'$date);// $date = $time[0].'年'.$time[1]."月".$time[0].'日';$date = date('Y年m月d日',$time);$this->returnCode(0,['week'=>$week,'date'=>$date]);}public function wx_login(){$code = Request::post('code');$appid = "wxdd3486cff1c813a6";$appSecret = "4fb84f7fd1f8fbac1554f60de272720e";if (!$code) {$this->returnCode(1,'微信授权失败,请刷新重试!');}# 根据code获取openid$url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$appSecret}&js_code={$code}&grant_type=authorization_code";$auth = file_get_contents($url);$auth = json_decode($auth,true);$openid = $auth['openid'];if(!isset($auth['openid'])){$this->returnCode(1,'微信授权失败,请刷新重试!');}$data['openid'] = $auth['openid'];$data['nickname'] = input('post.nickName');$data['avatar'] = input('post.avatarUrl');$data['province'] = input('post.province');$data['city'] = input('post.city');$data['sex'] = input('post.gender');$data['last_time'] = time();//如果已经绑定账号,直接登录,返回ticket$user = Db::table('oyk_user')->where('openid',$openid)->find();if($user && $user['status'] !=1 ){$this->returnCode(1,'账号已被禁用');}// 如果用户openid不存在,则添加if (empty($user)) {$data['add_time'] = time();$ret = Db::table('oyk_user')->insertGetId($data);$user = Db::table('oyk_user')->where('uid',$ret)->find();}else{Db::table('oyk_user')->where('openid',$openid)->update($data);}// 返回uid和openid$arr = ['uid' => $user['uid'],'openid' => $openid];$this->returnCode(0,$arr);}public function user_info(){$uid = Request::post('uid');$find = Db::table('oyk_user')->where('uid',$uid)->find();$y = date('Y',time());$start = $y.'-01-01';$end = $y.'-12-31';$zhichu = Db::table('oyk_jz')->where('uid','=',$uid)->whereTime('date','between',[$start,$end])->where('type','=',1)->sum('money');$shouru = Db::table('oyk_jz')->where('uid','=',$uid)->whereTime('date','between',[$start,$end])->where('type','=',2)->sum('money');$m = date('m',time());$start = $y.'-'.$m.'-01';$d = date('d',time());$end = $y.'-'.$m.'-'.$d;$yue = Db::table('oyk_jz')->where('uid','=',$uid)->whereTime('date','between',[$start,$end])->where('type','=',1)->sum('money');$arr = ['zhichu' => $zhichu,'shouru' => $shouru,'yue' => $find['money']-$yue,'find' => $find];$this->returnCode(0,$arr);}}



Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号