#### 功能介绍
**wxapp.php**是定义小程序获取数据或是操作数据的接口文件。小程序中一切与后端交流的数据都是通过API来实现。
#### 规范及约定
* Rcdonkey\_signup为模块标识,类名的定义遵循“模块标识ModuleWxapp”规则
* 此类必须继承 WeModuleWxapp 类
* 所有对外(小程序)公布的接口函数,必须是以**doPage**开头
这个文件就是小程序的后端文件。其结构大概如下:
1. `<?php`
2. `/**`
3. `* 接龙报名模块小程序接口定义`
4. `*`
5. `* @author 米粥`
6. `* @url`
7. `*/`
8. `defined('IN_IA') or exit('Access Denied');`
10. `class Rcdonkey_signupModuleWxapp extends WeModuleWxapp {`
11. `private function checkLogin() {`
12. `global $_W;`
13. `if (empty($_W['fans'])) {`
14. `return error(1, '请先登录');`
15. `}`
16. `return true;`
17. `}`
19. `/**`
20. `* 添加活动`
21. `*/`
22. `public function doPagePostActivity() {`
23. `//新增一个活动`
24. `global $_W, $_GPC;`
25. `$id = intval($_GPC['id']);`
26. `$login_success = $this->checkLogin();`
27. `if (is_error($login_success)) {`
28. `return $this->result($login_success['errno'], $login_success['message']);`
29. `}`
30. `if (empty($_GPC['title']) || empty($_GPC['description']) || empty($_GPC['joindeadline'])) {`
31. `return $this->result(2, '请填写完整活动内容');`
32. `}`
33. `$join_deadline = strtotime($_GPC['joindeadline']);`
34. `if (empty($join_deadline)) {`
35. `$join_deadline = strtotime("+7 day");`
36. `}`
37. `$require = explode(',', $_GPC['require']);`
38. `if (in_array('mobile', $require)) {`
39. `$require_mobile = 1;`
40. `}`
41. `if (in_array('realname', $require)) {`
42. `$require_realname = 1;`
43. `}`
44. `$data = array(`
45. `'title' => trim($_GPC['title']),`
46. `'description' => trim($_GPC['description']),`
47. `'join_deadline' => $join_deadline,`
48. `'join_total' => 0,`
49. `'organizer_openid' => $_W['fans']['openid'],`
50. `'organizer_nickname' => $_W['fans']['nickname'],`
51. `'organizer_avatar' => $_W['fans']['avatar'],`
52. `'longitude' => $_GPC['longitude'],`
53. `'latitude' => $_GPC['latitude'],`
54. `'status' => 1,`
55. `'createtime' => TIMESTAMP,`
56. `'require_realname' => $require_realname,`
57. `'require_mobile' => $require_mobile,`
58. `'fee' => floatval($_GPC['fee']),`
59. `);`
60. `if (empty($id)) {`
61. `if (pdo_insert('rcdonkey_activity', $data)) {`
62. `$id = pdo_insertid();`
63. `$code = $this->makeActivityCode($id);`
64. `if (!is_error($code)) {`
65. `pdo_update('rcdonkey_activity', array('code' => $code), array('id' => $id));`
66. `}`
67. `return $this->result(0, '发布成功', array('id' => $id));`
68. `}`
69. `} else {`
70. `$activity = $this->getActivity($id);`
71. `if ($_W['openid'] != $activity['organizer_openid']) {`
72. `return $this->result(2, '您没有权限修改该活动');`
73. `}`
74. `$data = array(`
75. `'title' => trim($_GPC['title']),`
76. `'description' => trim($_GPC['description']),`
77. `'join_deadline' => $join_deadline,`
78. `);`
79. `pdo_update('rcdonkey_activity', $data, array('id' => $id));`
80. `return $this->result(0, '修改成功', array('id' => $id));`
81. `}`
82. `return $this->result(3, '发布失败,请重试');`
83. `}`
85. `public function doPageDetail() {`
86. `global $_W, $_GPC;`
87. `$id = intval($_GPC['id']);`
88. `if (empty($id)) {`
89. `$this->result(1, '活动不存在或是已经被删除');`
90. `}`
91. `$activity = $this->getActivity($id);`
93. `$join_user = pdo_get('rcdonkey_join_list', array('openid' => $_W['openid'], 'activity_id' => $id));`
94. `$activity['already_join'] = !empty($join_user) ? true : false;`
95. `$this->result(0, '', $activity);`
96. `}`
98. `private function getActivity($id) {`
99. `global $_W;`
100. `if (empty($id)) {`
101. `return array();`
102. `}`
103. `$activity = pdo_get('rcdonkey_activity', array('id' => $id));`
104. `if (empty($activity)) {`
105. `return error(1, '活动不存在或是已经被删除');`
106. `}`
107. `$activity['description'] = emoji_unicode_decode($activity['description']);`
109. `if (empty($activity['code'])) {`
110. `$code = $this->makeActivityCode($id);`
111. `if (!is_error($code)) {`
112. `pdo_update('rcdonkey_activity', array('code' => $code), array('id' => $id));`
113. `}`
114. `$activity['code'] = $code;`
115. `} else {`
116. `$activity['code'] = tomedia($activity['code']);`
117. `}`
119. `if ($activity['status'] == 3) {`
120. `$activity['join_deadline'] = TIMESTAMP - 38400;`
121. `}`
122. `$activity['join_deadline_date'] = date('Y-m-d H:i', $activity['join_deadline']);`
123. `$activity['join_list'] = pdo_getall('rcdonkey_join_list', array('activity_id' => $id), array(), 'openid', 'id DESC');`
124. `if (!empty($activity['join_list'])) {`
125. `foreach ($activity['join_list'] as $i => &$join_user) {`
126. `$join_user['join_date'] = date('Y-m-d H:i', $join_user['createtime']);`
127. `}`
128. `}`
129. `if ($_W['openid'] == $activity['organizer_openid']) {`
130. `$activity['is_manager'] = true;`
131. `}`
133. `if ($activity['join_deadline'] < TIMESTAMP) {`
134. `pdo_update('rcdonkey_activity', array('status' => 3), array('id' => $id));`
135. `$activity['status'] = 3;`
136. `}`
137. `return $activity;`
138. `}`
139. `}
#### 结果返回
由于小程序中沟通数据是采用API的形式,所以数据都是以JSON的形式进行返回,在这里可以直接调用**$this->result()**来返回结果,[具体查看函数说明](http://s.w7.cc/%E3%80%82/index.php?c=wiki&do=view&id=1&list=514 "具体查看函数说明")
#### 小程序端如何请求
所有对外(小程序)公布的接口函数,必须是以**doPage**开头,比如下方实例中**doPagePostActivity()**,表示新增一个活动,在小程序中直接使用**app.util.request()**函数来请求。代码如下,其它函数引用同理。
1. `app.util.request({`
2. `url: 'entry/wxapp/postactivity',`
3. `data: {`
4. `m: 'rcdonkey_signup',`
5. `title : form.title,`
6. `/*省略部分代码*/`
7. `},`
8. `method : 'post',`
9. `success: function (response) {`
10. `/*省略部分代码*/`
11. `},`
12. `fail : function(response) {`
13. `if (response.data.message) {`
14. `app.util.message(response.data.message, '', 'error');`
15. `}`
16. `}`
17. `});`
- 微信消息
- 概述
- 响应流程
- 入门指引
- 安装微擎
- 环境安装
- 百度云(微擎镜像)
- 宝塔(Linux)推荐
- 注册云服务
- 代码规范
- php编码规范
- 数据库设计
- 文件目录规范
- JavaScript编辑码规范
- 微擎框架
- 目录结构
- 数据库字典
- 配置选项
- 数据库配置
- 系统COOKIE配置项
- 系统设置
- 上传配置 (已废弃,统一在系统附件设置中更改)
- 预定义常量
- 全局变量
- 全局配置
- 系统配置
- 公众号相关
- Web 端可见
- App 端可见
- 网页授权
- URL 路由
- url
- createMobileUrl
- createWebUrl
- 文件加载器
- func
- class
- model
- web
- app
- library
- object
- singleton
- 缓存机制
- cache_write
- cache_load
- cache_delete
- cache_clean
- cache_system_key
- 数据库
- SQL注入安全
- 连接其它数据库
- 数据库函数列表
- pdo_get
- pdo_getcolumn
- pdo_getall
- pdo_getslice
- pdo_fetch
- pdo_fetchcolumn
- pdo_fetchall
- pdo_insert
- pdo_update
- pdo_delete
- pdo_query
- pdo_run
- pdo_fieldexists
- pdo_indexexists
- pdo_tableexists
- pdo_fieldmatch
- pdo_debug
- 链式查询
- from
- select
- where
- whereor
- having
- groupby
- orderby
- leftjoin
- innerjoin
- on
- limit
- page
- get
- getall
- getcolumn
- count
- exists
- getLastQueryTotal
- getLastQuery
- 错误处理
- 日志记录
- 模板引擎
- 模板标签
- 输出变量
- 判断语句
- 循环语句
- php语句
- 引用模板
- 创建URL
- 转义附件URL
- 输出花括号
- 数据调用
- 调用微站导航
- 调用幻灯片
- 调用文章列表
- 调用文章评论
- 调用分类列表
- 调用快捷菜单
- 常用变量
- 函数列表
- 系统公共函数
- istripslashes
- ihtmlspecialchars
- isetcookie
- token
- random
- checksubmit
- checkcaptcha
- tablename
- array_elements
- range_limit
- ijson_encode
- iserializer
- iunserializer
- is_base64
- is_serialized
- wurl
- murl
- pagination
- tomedia
- referer
- strexists
- cutstr
- istrlen
- emotion
- authcode
- sizecount
- bytecount
- array2xml
- xml2array
- scriptname
- utf8_bytes
- isimplexml_load_string
- aes_decode
- aes_encode
- ihtml_entity_decode
- iarray_change_key_case
- parse_path
- strip_gpc
- to_global_media
- 参数安全函数
- 业务公共函数
- Http请求函数
- ihttp_request
- ihttp_get
- ihttp_post
- ihttp_email
- 文件操作函数
- file_write
- file_read
- file_move
- file_tree
- file_copy
- file_upload
- file_wechat_upload
- file_remote_upload
- file_random_name
- file_delete
- file_remote_delete
- file_image_thumb
- file_image_crop
- file_lists
- file_remote_attach_fetch
- file_is_image
- mkdirs
- rmdirs
- 数据库维护函数
- db_table_schema
- db_table_serialize
- db_table_create_sql
- db_schema_compare
- db_table_fix_sql
- _db_build_index_sql
- _db_build_field_sql
- db_table_schemas
- db_table_insert_sql
- Web端公共函数
- url web
- message
- checklogin
- checkaccount
- template
- App端公共函数
- url app
- checkauth
- message app
- template app
- register_jssdk
- 安全操作函数
- 模板组件列表
- Web端组件
- _tpl_form_field_date
- tpl_form_field_link
- tpl_form_module_link
- tpl_form_field_emoji
- tpl_form_field_color
- tpl_form_field_icon
- tpl_form_field_image
- tpl_form_field_multi_image
- tpl_form_field_audio
- tpl_form_field_multi_audio
- tpl_form_field_video
- tpl_form_field_wechat_image
- tpl_form_field_wechat_multi_image
- tpl_form_field_wechat_voice
- tpl_form_field_wechat_video
- tpl_form_field_location_category
- tpl_ueditor
- tpl_edit_sms
- tpl_coupon_colors
- App端组件
- _tpl_form_field_date app
- tpl_app_fans_form
- tpl_app_form_field_calendar
- tpl_app_form_field_district
- tpl_app_form_field_avatar
- tpl_app_form_field_image
- tpl_app_coupon_item
- 系统公共组件
- tpl_form_field_date
- tpl_form_field_clock
- tpl_form_field_daterange
- tpl_form_field_calendar
- tpl_form_field_district
- tpl_form_field_category_2level
- tpl_form_field_industry
- tpl_form_field_coordinate
- tpl_fans_form
- 支付API
- 发起支付(PHP)
- 发起支付(JS)
- 验证支付
- 退款(微信、支付宝)
- 微信API
- 基础
- 粉丝(公众号)
- fansAll
- fansQueryInfo
- fansBatchQueryInfo
- 粉丝标签(公众号)
- fansTagAdd
- fansTagFetchAll
- fansTagEdit
- fansTagDelete
- fansTagGetUserlist
- fansTagTagging
- fansTagBatchTagging
- fansTagBatchUntagging
- fansTagFetchOwnTags
- 自定义菜单(公众号)
- menuCreate
- menuDelete
- menuCurrentQuery
- menuQuery
- 二维码(公众号)
- barCodeCreateDisposable
- barCodeCreateFixed
- 客服消息(公众号)
- 模板消息(公众号)
- 群发(公众号)
- fansSendAll
- fansSendPreview
- 临时素材(公众号)
- uploadMedia
- downloadMedia
- 永久素材(公众号)
- uploadMediaFixed
- addMatrialNews
- uploadNewsThumb
- uploadVideoFixed
- editMaterialNews
- getMaterial
- getMaterialCount
- delMaterial
- batchGetMaterial
- 短网址(公众号)
- 共享收货地址(公众号)
- JSSDK(公众号)
- 自定义分享(公众号)
- 小程序码
- getCodeLimit
- getCodeUnlimit
- 云服务API
- 本地调试
- 键值对存储
- 查询模块信息
- 查询站点信息
- 发短信
- 常见问题
- 设置开发模式
- 调试sql语句
- 微信图片盗链
- mac系统压缩包无法上传
- reditect_url 参数错误
- GIT发布(上传大文件)
- 共享包
- iframe跨域解决方案
- 更新出错如何恢复
- 小程序开发
- 与微擎通信
- 模块中定义文件
- 接口验签
- PHP接口返回值
- 接口版本
- session机制
- 发起支付
- util.js工具库
- util.url
- util.request
- util.getUserInfo
- util.navigateBack
- util.footer
- util.message
- util.showLoading
- util.showImage
- util.parseContent
- util.date
- isLeapYear
- dateToStr
- dateAdd
- dateDiff
- dateToLong
- longToDate
- isDate
- getMaxDay
- toArray
- datePart
- maxDayOfDate
- util.md5
- util.base64Encode
- util.base64Decode
- app.js
- 获取用户信息
- 应用开发
- 机制说明
- 命名空间
- 定义微擎首页
- 智能应答
- 自定义分享
- 分离代码
- 粉丝信息
- manifest.xml
- install.php和upgrade.php
- 设计应用
- 全局变量1
- MODULE_URL
- MODULE_ROOT
- $_W[current_module]
- 安装、更新与卸载
- 发布应用
- 功能文件
- module.php
- site.php
- wxapp.php
- result
- webapp.php
- mobileapp.php
- hook.php
- receiver.php
- systemwelcome.php
- 支付
- 在php中发起支付
- 在微信浏览器中发起
- 在小程序中发起
- 验证支付结果
- 云商城常见问题
- 发布应用1
- 应用文档结构
- manifest.xml -
- 发布应用错误提示
- git 大文件发布
- 云Api发布
- 对外接口
