Drupal 使用 wechat + Node Gallery + Profile2 实现微信发送图片自动上传到指定相册
🏷️ Drupal
1. 安装【wechat + Node Gallery + Profile2】三个模块并启用
wechat:微信相关的基础模块
Node Gallery:相册模块,可以实现相册功能
Profile2:可以在账户里追加自定义的设定项
2. 利用 Profile2 在账户设置画面追加上传图片时的默认相册
在【 admin/structure/profiles/manage/main/fields
】页面追加字段【微信上传默认相册(机读名称:field_wechat_default_gallery
)】;
类型为 Entity Reference;
ENTITY SELECTION→模式:Simple(with optinal filter by bundle)
Target bundles:勾选相册(node_gallery_gallery);
保存字段设置;
3. 创建一个默认相册
假设 nid
为 17,若用户未设置则自动上传到该相册
4. 修改 wechat.module 文件,实现上传时自动追加一个相册文件 (node_gallery_item) 功能
在 wechat_build_request_message
方法的【 $xml_obj->MsgType == 'image'
】 if
分支的最后追加如下代码:
php
//根据 openid 获取 wechat 用户相关信息
$openid = $xml_obj->FromUserName;
$wechatuser = wechat_wechatuser_load($openid);
$galleryitem = node_gallery_api_create_item_from_wechat_image($uri, $message_wrapper->msg_id->value() . ".jpg", $wechatuser);
node_gallery_api_create_item_from_wechat_image
方法参照了 node gallery 模块的 node_gallery_api_create_item_from_file
方法:
点击查看代码
php
/**
* Create item from file. Form Plupload.
* node_gallery_api_create_item_from_file
*/
function node_gallery_api_create_item_from_wechat_image($uri, $original_filename, $wechatuser) {
//未关注用户不允许上传图片
if (empty($wechatuser)) {
return FALSE;
}
//根据 uid 获取用户信息,并赋值到全局 user 变量
global $user;
$user = user_load($wechatuser->uid);
//根据 uid 获取 profile2 模块中追加的额外设置项,这里是用户设置的默认上传相册
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'profile2', '=')
->propertyCondition('uid', $user->uid, '=');
$result = $query->execute();
$pids = array();
foreach($result as $record) {
$pids = $record->pid;
}
$profiles = entity_load("profile2", $pids);
$profile = reset($profiles);
//未设置就上传到默认相册 (这里的默认相册 nid 是 17)
$relationship_type_id = 1;
$ngid = $profile->field_wechat_default_gallery ? $profile->field_wechat_default_gallery : "17";
$item_type = "node_gallery_item";
$relationship_type = node_gallery_api_get_relationship_type(NULL, NULL, $relationship_type_id);
$instance = field_info_instance('node', $relationship_type->filefield_name, $item_type);
// Get file schema from field settings.
$scheme = variable_get('file_default_scheme', 'public') . '://';
$fields = field_info_fields();
if (!empty($fields)) {
$scheme = $fields . '://';
}
if (!empty($instance)) {
$destination_dir = $scheme . token_replace($instance);
$destination_filename = $destination_dir . '/' . $original_filename;
}
else {
$destination_filename = $scheme . $original_filename;
}
file_prepare_directory($destination_dir, FILE_CREATE_DIRECTORY);
$destination = file_stream_wrapper_uri_normalize($destination_filename);
// $destination = file_unmanaged_move($uri, $destination, FILE_EXISTS_RENAME);
// 复制图片到相册文件目录
$destination = file_unmanaged_copy($uri, $destination, FILE_EXISTS_RENAME);
$file = wechat_file_uri_to_object_cus($destination);
$file->display = 1;
file_save($file);
//新生成一个相册文件
$item = new stdClass();
$item->type = $item_type;
$item->uid = $user->uid;
$item->name = $user->name;
$item->title = $original_filename;
$item->{$relationship_type->filefield_name} = (array) $file;
$fieldname = node_gallery_api_get_item_field_name(NULL, NULL, $relationship_type->id);
$item->{$fieldname} = $ngid;
// Get gallery published status and set item to be the same.
$item->status = db_select('node', 'n')->fields('n', array('status'))->condition('n.nid', $ngid)->execute()->fetchField();
$item->language = LANGUAGE_NONE;
node_object_prepare($item);
node_save($item);
return $item;
}
/**
* Returns a file object which can be passed to file_save().
*
* @param string $uri
* A string containing the URI, path, or filename.
*
* @return boolean
* A file object, or FALSE on error.
*
* @todo Replace with calls to this function with file_uri_to_object() when
* http://drupal.org/node/685818 is fixed in core.
*/
function wechat_file_uri_to_object_cus($uri) {
global $user;
// $uri = file_stream_wrapper_uri_normalize($uri);
// $wrapper = file_stream_wrapper_get_instance_by_uri($uri);
$file = new StdClass();
$file->uid = $user->uid;
$file->filename = drupal_basename($uri);
$file->uri = $uri;
$file->filemime = file_get_mimetype($uri);
// This is gagged because some uris will not support it.
$file->filesize = @filesize($uri);
$file->timestamp = REQUEST_TIME;
$file->status = FILE_STATUS_PERMANENT;
return $file;
}